JavaScript Fundamentals / JavaScript Basics

Operators

➕ Arithmetic operator

An arithmetic operator accepts numerical values as operands and returns a single numerical value. The numerical values can be literals or variables.

⚡️ Addition operator (+)

The addition operator returns the sum of two values.

The following uses the addition operator to concatenate two strings:

let sum = 10 + 20;
console.log(sum);  // 30

let x = '10',
    y = '20';
let result = x + y;
console.log(result);  //1020

⚡️Subtraction operator (-)

For example:

And If a value is a string, a boolean, null, or undefined, the JavaScript engine will:

  1. First, convert the value to a number using the Number() function.

  2. Second, perform the subtraction.

let result = 30 - 10;
console.log(result);// 20

⚡️Multiplication operator (*)

JavaScript uses the asterisk (*) to represent the multiplication operator. The multiplication operator multiplies two numbers and returns a single value. For example:

If either value is not a number, the JavaScript engine implicitly converts it into a number using the Number() function and perform the multiplication.

let result = 2 * 3;
console.log(result); //6
let result = '5' * 2;
console.log(result); //10

⚡️ Divide operator (/)

Javascript uses the slash (/) character to represent the divide operator. The divide operator divides the first value by the second one. For example:

let result = 20 / 10;
console.log(result);// 2

If either value is not a number, the JavaScript engine converts it into a number for division. For example:

let result = '20' / 2;
console.log(result);// 10;

⚡️ Modulus operator (%)

The addition operator returns the sum of two values.

The following uses the addition operator to concatenate two strings:

let mod = 10 % 20;
console.log(mod);  // 10

🟰 Assignment operator

Assignment operator(=) are used to assign values to variables in JavaScript.

x = 10
y = 20
x = y ;
console.log(x); //20
console.log(y); //20

⚡️Addition Assignment Operator(+=)

The Addition assignment operator adds the value to the right operand to a variable and assigns the result to the variable. Addition or concatenation is possible. In case of concatenation then we use the string as an operand.

let a = 2;
const b = 3;
console.log(a += b );  //5

⚡️Subtraction Assignment Operator(-=)

The Subtraction Assignment Operator subtracts the value of the right operand from a variable and assigns the result to the variable.

let y = 4;
console.log(y -= 1); //3

⚡️Remainder Assignment Operator(%=)

The Remainder Assignment Operator divides a variable by the value of the right operand and assigns the remainder to the variable.

let Rem = 50;
console.log(Rem %= 10); //0

⚡️Exponentiation Assignment Operator(**=)

The Exponentiation Assignment Operator raises the value of a variable to the power of the right operand.

let Exp = 2;
console.log(Exp **= 3); //8

⚡️Division Assignment Operator(/=)

The Division Assignment operator divides a variable by the value of the right operand and assigns the result to the variable.

let Div = 10;
const m = 2;
console.log(Div /= m); //5

⚡️Multiplication Assignment Operator(*=)

✍️ The Multiplication Assignment operator multiplies a variable by the value of the right operand and assigns the result to the variable.

let M = 4;
console.log(M *= 2); //8

⚖️ Comparison Operators

⚡️Equality Operator (==)

The Equality operator is used to compare the equality of two operands.

let x = 55;
let y = 55;
console.log(x == 5); //true
console.log(y == 5); //true
console.log(x == y); //true

⚡️Inequality Operator (!=)

The Inequality Operator is used to compare the inequality of two operands.

let x = 9;
let y = 7;
console.log(x != 6); //true
console.log(y != 7);  //false
console.log(x != y);  //false

⚡️Strict equality Operator (===)

The Strict equality Operator is used to compare the equality of two operands with type.

let x = 33;
let y = 48;
console.log(x === 63); //false
console.log(y === 48);  //true
console.log(x === y);  //false

⚡️Strict inequality Operator (!==)

The Strict inequality Operator is used to compare the inequality of two operands with type.

let x = 5;
let y = 5;
console.log(x !== 6); //true
console.log(y !== 5);  //false
console.log(x !== y);  //true

⚡️Greater than Operator (>)

The Greater than Operator is used to check whether the left-side value is greater than the right-side value.

let x = 2;
let y = 3;
console.log(x > 16); //false
console.log(y > 15);  //false

⚡️Greater than or equal Operator (>=)

The Greater than or equal Operator is used to check whether the left side operand is greater than or equal to the right side operand.

let x = 1;
let y = 4;
console.log(x >= 5); //true
console.log(y >= 15);  //false

⚡️Less than Operator (<)

The Less than Operator is used to check whether the left-side value is less than the right-side value.

let x = 6;
let y = 8;
console.log(x < 5); //true
console.log(y < 15);  //true

⚡️Less than or equal Operator (<=)

The Less than or equal Operator is used to check whether the left side operand value is less than or equal to the right side operand value.

let val1 = 5;
let val2 = "5";
console.log(val1 <= 15); //true
console.log(val2 <= "0"); //false

‼ Logical Operators

⚡️ Logical NOT operator (!)

JavaScript uses an exclamation point ! to represent the logical NOT operator. The ! operator can be applied to a single value of any type, not just a Boolean value.

When you apply the ! operator to a boolean value, the ! returns true if the value is false and vice versa. For example:

let eligible = false,
    required = true;

console.log(!eligible);  //true
console.log(!required);  //false

The logical ! operator works based on the following rules:

  • If a is undefined, the result is true.

  • If a is null, the result is true.

  • If a is a number other than 0, the result is false.

  • If a is NaN, the result is true.

  • If a is an object, the result is false.

  • If a is an empty string, the result is true. If a is a non-empty string, the result is false

⚡️Double-negation (!!)

Sometimes, you may see the double negation (!!) in the code. The !! uses the logical NOT operator (!) twice to convert a value to its real boolean value. For example:

let counter = 10;
console.log(!!counter);// true
  • The first ! operator negates the Boolean value of the counter variable. If the counter is true, then the ! operator makes it false and vice versa.

  • The second ! operator negates the result of the first ! operator and returns the real boolean value of the counter variable.

⚡️Logical AND operator (&&)

JavaScript uses the double ampersand (&&) to represent the logical AND operator.

The result of the && operator is true only if both values are true, otherwise, it is false. For example:

let eligible = false,
    required = true;

console.log(eligible && required); // false
let eligible = true,
    required = true;

console.log(eligible && required); // true

⚡️The chain of && operators

The following expression uses multiple && operators:

let result = value1 && value2 && value3;

The && operator carries the following:

  • Evaluates values from left to right.

  • For each value, convert it to a boolean. If the result is false, stops and returns the original value.

  • If all values are truthy values, return the last value

⚡️Logical OR operator (||)

JavaScript uses the double-pipe || to represent the logical OR operator.

The || operator returns false if both values are evaluated to false. In case either value is true, the || operator returns true. For example:

let eligible = true,
    required = false;

console.log(eligible || required); // true

⚡️The chain of || operators

The following example shows how to use multiple || operators in an expression:

let result = value1 || value2 || value3;

The || operator does the following:

  • Evaluates values from left to right.

  • For each value, convert it to a boolean value. If the result of the conversion is true, stops and returns the value.

  • If all values have been evaluated to false, returns the last value.