Javascript operators

Javascript operators

Operators serve as the building blocks of expressive and efficient code. From performing arithmetic calculations to manipulating strings and comparing values, operators empower developers to wield the full potential of the language.

Arithmetic operators

Arithmetic operators enable us to perform mathematical calculations in JavaScript. They include addition (+), subtraction (-), multiplication (*), division (/), and Modulus (%) more. Let's dive into some examples:

let num1 = 7;
let num2 = 4;

let sum = num1 + num2; // Addition
let difference = num1 - num2; // Subtraction
let product = num1 * num2; // Multiplication
let quotient = num1 / num2; // Division
let remainder = num1 % num2 //modulus or remainder

console.log(sum, difference, product, quotient, remainder); 
// Outputs: 11, 3, 28, 1, 3

These are the basic operations and I firmly believe that you would be aware of these operations or you can understand from the above example.

Assignment operator

Assignment operators are used to assign values to variables. They include the simple assignment (=) as well as compound assignment operators such as +=, -= etc. Here's an example:

let count = 10;
count += 5; // Equivalent to: count = count + 5
console.log(count); // Outputs: 15

let anotherCount = 10;
count -= 5; //Equivalent to: count = count - 5
console.log(anotherCount); //outputs: 5

Comparison operator

Comparison operators allow us to compare values and determine their relationship. They include equality (==, ===), here's an example:

1) Double equals: The output of x == y depends on the values of x and y.

In JavaScript, the double equals (==) operator checks for equality between two values. It performs type conversion if necessary, meaning it tries to convert the operands to the same type before making the comparison.

Let's consider an example:

let x = 10;
let y = "10";

console.log(x == y); // Outputs: true

2) Triple equals: The triple equals (===) operator in JavaScript is called the "strict equality" operator. It checks if two values are equal in both value and data type. If the values are of different data types, they are considered unequal, even if their values look the same when converted to the same type.

Let's consider an example:

let x = 10;
let y = "10";

console.log(x === y); // Outputs: false

In this example, x is a number (10), and y is a string ("10"). Even though they both have the same value 10, they are of different data types. The strict equality operator (===) checks both the value and the data type. Since x and y are of different types (number and string), x === y evaluates to false.

If you want to check for equality regardless of data type (only comparing the value), you can use the double equals (==) operator. However, it's generally recommended to use strict equality (===) to avoid unexpected behavior due to type coercion.

Some complex conversions

1) console.log(1 + "2"); //outputs: 12
2) console.log("1" + 2); //outputs: 12
3) console.log("1" + 2 + 2); // outputs: 122
4) console.log(1 + 2 + "2");// outputs: 32
  1. JavaScript automatically converts the number 1 to a string and concatenates it with the string "2", resulting in "12". When one operand is a string, JavaScript converts the other operand to a string as well, leading to string concatenation.

  2. JavaScript treats the string "1" as a string and concatenates it with the number 2, resulting in "12". Once again, type conversion occurs, converting the number 2 into a string for concatenation.

  3. In this case, the string "1" is concatenated with the number 2, resulting in "12". Then, the resulting string "12" is further concatenated with the number 2, producing "122". JavaScript performs left-to-right evaluation, applying type conversion as needed.

If js sees string at first, then it treats the rest of the operands as string, that's why it gave output 122 instead of 14.

  1. JavaScript first performs addition on the numbers 1 and 2, resulting in 3. Then, it concatenates the resulting number 3 with the string "2", yielding "32". In this case, JavaScript prioritizes the numeric addition before the string concatenation.