DEV Community

Cover image for Basic operators in JavaScript
aishwaryavasu0509
aishwaryavasu0509

Posted on

Basic operators in JavaScript

operators

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

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

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!!`);
Enter fullscreen mode Exit fullscreen mode

}

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

{

  console.log(`its a no`);
Enter fullscreen mode Exit fullscreen mode

}

else

{

console.log(`yes`);
Enter fullscreen mode Exit fullscreen mode

}

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

Discussion (0)