Unleash the Magic: Exploring JavaScript Operators for Smoother Coding(lt.31)

himanshuhimanshu
2 min read

Table of contents

Operators are symbols that perform some operations on one or more values (called operands) and produce a result.

  1. Arithmetic Operators: These operators perform arithmetic operations on numerical values.

    • Addition +

    • Subtraction -

    • Multiplication *

    • Division /

    • Modulus % (returns the remainder of a division)

  2. Assignment Operators: These operators assign values to variables.

    • Assignment =

    • Addition assignment +=

    • Subtraction assignment -=

    • Multiplication assignment *=

    • Division assignment /=

  3. Comparison Operators: These operators compare values and return a boolean result .

    • Equal to == or strict equal to ===

    • Not equal to != or strict not equal to !==

    • Greater than >

    • Less than <

    • Greater than or equal to >=

    • Less than or equal to <=

  4. Logical Operators: These operators perform logical operations and return a boolean result.

    • Logical AND &&

    • Logical OR ||

    • Logical NOT !

  5. Unary Operators: These operators work on a single operand.

    • Unary plus (converts an operand into a number)

    • Unary minus (negates an operand)

    • Increment (increments the value by 1)

    • Decrement (decrements the value by 1)

  6. Conditional (Ternary) Operator: This operator is a shorthand for an if...else statement.

    • condition ? expression1 : expression2
  7. String Operators: JavaScript allows string concatenation using the + operator.

  8. Spread Operator: The spread operator in JavaScript (often denoted by ...) is a powerful feature used for various purposes like copying arrays, merging arrays, and spreading elements in function calls.

    Generally array is copied as a reference in js so any changes in the first array will be reflected back to the second array so to avoid this we use spread operator , it will become more clear from the below given code.

Program to demonstrate :

console.log(10+20);
console.log(10-20);
console.log(10*20);
console.log(10/20);
console.log(101%20);


a = 10
b=20
console.log(a+b);


let m= 20;
console.log(m)
m += 10;
console.log(m)


let a1= 20
a2 = 10
console.log(a1==a2)
console.log(a1>a2)

let s1 = 400
let s2 ="400"
console.log(s1==s2)
// == refers to direct value checking
console.log(s1===s2)
// but === refers to value + type checking


let v1 = true
let v2 = false
console.log(v1 && v2)


let j = 100
let k = 10
console.log(j>k ? true: false)



let v = [1,2,3]
let n =v
console.log(v)
console.log(n)
v[0]=90
console.log(v)
console.log(n)

console.log("after using spread operator")

let v11 = [1,2,3]
let n11 =[...v11]
console.log(v11)
console.log(n11)
v11[0]=90
console.log(v11)
console.log(n11)

lt.30link: https://hashnode.com/post/cltbxg4qk000108l90lfnf4gg

10
Subscribe to my newsletter

Read articles from himanshu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

himanshu
himanshu