Basic operators in JavaScript
Tags: operators, javascript
Published: 2021-06-24 05:43:53

![operators](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hx21jw4lsw5ub8aq5tly.png)


##what is an operator?

An operator performs some operation on single or multiple operands (data value) and produces a result. 

example:
const firstYear=2037;
const ageJonas = firstYear-656
const ageSarah = firstYear-2018

console.log(ageJonas,ageSarah) //logging multiple values.
console.log(ageJonas*2,ageSarah*3); // multiplying
console.log(2**3,ageSarah/10)//dividing the values


###string concatination

![string concatination](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cvitipj3z9m23jg1e4e.jpg)

This method is used to join two or more strings. This method does not change the existing strings, but returns a new string containing the text of the joined strings.

example:

const firstName="Jonas";
const latName="node";
console.log(firstName+' '+lastName);


###assignment operator

![assignment operator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6cz6vwh48x8jypz17xza.png)

An assignment operator assigns a value to its left operand based on the value of its right operand. 

example:

let x=15;
let y=56;

console.log(x+y,x-y);

let z;
z=x+y;

consloe.log(z+y);

x+=10 //x=x+10
y+=20//y=y+20
x++//single incremetor
x--//single decrementor


###comparison operators (>,<,===,>=,<=)

Comparison operators are used in logical statements to determine equality or difference between variables or values.

example:

console.log(ageSarah > ageJonas) // either true or false
console.log(ageSarah>=18);
console.log(ageJonas>=20);

let islarger = ageSarah >=ageJonas; // stores value as a boolean

###Equality Operators: == vs. ===

In one word, main difference between "==" and "===" operator is that formerly compares variable by making type correction e.g. if you compare a number with a string with numeric literal, == allows that, but === doesn't allow that, because it not only checks the value but also type of two variable, if two variables are not of the same type "===" return false, while "==" return true. 

examples:

const age =`18`;

if(age ===18) console.log(`you just became an adult :D(strict)`);

// strict equality check with the age and it will yield in the message

if(age == 18) console.log(`you just become an adult :D(llose`));

console.log(a = b = 59);
// expected output: 59

###Logical Operators

Logical operators are used to determine the logic between variables or values.

example:

const hasDriversLiscense = true;
const hasGoodVision = false;

//the logical and operator

console.log(hasDriversLiscense && hasGoodVision);

//the logical or operator

console.log(hasDriversLiscense || hasGoodVision);

//the logical not operator

console.log(!hasDriversLiscense);


if(hasDriversLiscense && hasGoodVision) {

console.log(`sarah is able to drive`);

}

else{

console.log(`she must not frive`);

}



//logical operations with more than two boolean variables

const isTired = False;

console.log(hasDriversLiscense && hasGoodVision||isTired)

// true||true returns true

if(hasDriversLiscense && hasGoodVision&&isTired)

{

     console.log(`probably no!!`);

}

else if ((hasDriversLiscense && hasGoodVision||isTired)

{

      console.log(`its a no`);

}

else

{

    console.log(`yes`);

}

###The Conditional (Ternary) Operator

ternary operator:

let age=21

const drink = age >=18 ? 'wine':'water';

console.log(drink);

## Operator precedence in javascript

Operator precedence determines how operators are parsed concerning each other. Operators with higher precedence become the operands of operators with lower precedence.

example:

console.log(42 * 3 ** 3); // 42 * 27
// expected output: 1134

let a;
let b;

[operator precedence and associativity in javascript](https://www.geeksforgeeks.org/operator-precedence-in-javascript/)