Operator

DevJunctionPoint
0

What is an Operator?

  • In JavaScript, an operator is a special symbol used to perform operations on operands (values and variables).

example:

      2 + 3 // 5

  • Here + is Operator that performs addition, and 2 and 3 are Operands.

JavaScript Operator Types

  • Assignment Operators
  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Increment Operator
  • Decrement Operator
  • Ternary Operators 

Assignment Operators
  • Assignment operators are used to assign values to variables.


    const x = 5;


  • the = operator is used to assign the value 5 to variable x



Arithmetic Operators
     Arithmetic operators are used to perform arithmetic calculations
  • Addition(+): a + b
  • Subtraction(-): a - b
  • Multiplication(*): a * b
  • Division(/): a / b
  • Modulus(%): a % b
  • Exponential(**): a ** b
  • Increment (increments by 1): ++a,a++ 
  • Decrement (decrements by 1) : --a,a--


    let numOne = 4
    let numTwo = 3
    let sum = numOne + numTwo // 7
    let diff = numOne - numTwo // 1
    let mult = numOne * numTwo // 12
    let div = numOne / numTwo // 1.333
    let remainder = numOne % numTwo // 1
    let powerOf = numOne ** numTwo // 64

    console.log(sum, diff, mult, div, remainder, powerOf)
    
    output:7 1 12 1.3333333333333333 1 64
            


Increment Operator
  • In JavaScript, we use the increment operator to increase a value stored in a variable.
  • The increment could be pre or post-increment.
Pre-increment


    let count = 0
    console.log(++count)        // 1 -- Pre increment
    console.log(count)          // 1


Post-increment


    let count = 0
    console.log(count++)        // 0 -- Post Increment
    console.log(count)          // 1


Decrement Operator
  • In JavaScript, we use the decrement operator to decrease a value stored in a variable. 
  • The decrement could be pre or post-decrement
Pre-decrement


     let count = 0
    console.log(--count) // -1
    console.log(count)  // -1


Post-decrement


     let count = 0
    console.log(count--) // 0
    console.log(count)   // -1


Comparison Operators
  • Comparison operators compare two values and return a boolean value, either true or false



console.log(3 > 2)              // true, because 3 is greater than 2
console.log(3 >= 2)             // true, because 3 is greater than 2
console.log(3 < 2)              // false,  because 3 is greater than 2
console.log(2 < 3)              // true, because 2 is less than 3
console.log(2 <= 3)             // true, because 2 is less than 3
console.log(3 == 2)             // false, because 3 is not equal to 2
console.log(3 != 2)             // true, because 3 is not equal to 2
console.log(3 == '3')           // true, compare only value
console.log(3 === '3')          // false, compare both value and data type
console.log(3 !== '3')          // true, compare both value and data type
console.log(3 != 3)             // false, compare only value
console.log(3 !== 3)            // false, compare both value and data type
console.log(0 == false)         // true, equivalent
console.log(0 === false)        // false, not exactly the same
console.log(0 == '')            // true, equivalent
console.log(0 == ' ')           // true, equivalent
console.log(0 === '')           // false, not exactly the same
console.log(1 == true)          // true, equivalent
console.log(1 === true)         // false, not exactly the same
console.log(undefined == null)  // true
console.log(undefined === null) // false
console.log(NaN == NaN)         // false, not equal
console.log(NaN === NaN)        // false
console.log(typeof NaN)         // number



Logical Operators
  • Logical operators perform logical operations and return a boolean value, either true or false

// && ampersand operator example

const check = 4 > 3 && 10 > 5         // true && true -> true
const check = 4 > 3 && 10 < 5         // true && false -> false
const check = 4 < 3 && 10 < 5         // false && false -> false

// || pipe or operator, example

const check = 4 > 3 || 10 > 5         // true  || true -> true
const check = 4 > 3 || 10 < 5         // true  || false -> true
const check = 4 < 3 || 10 < 5         // false || false -> false

//! Negotiation examples

let check = 4 > 3                     // true
let check = !(4 > 3)                  //  false
let isLightOn = true
let isLightOff = !isLightOn           // false
let isMarried = !false                // true


Bitwise Operators

     Bitwise operators perform operations on binary representations of numbers
  • Operator Description
  • &         Bitwise AND
  • |         Bitwise OR
  • ^         Bitwise XOR
  • ~         Bitwise NOT
  • <<         Left shift
  • >>         Sign-propagating right shift
  • >>>         Zero-fill right shift
Ternary Operators
  • Ternary operator allows to write a condition. Another way to write conditionals is by using ternary operators.

    let number = 5
    number > 0
      ? console.log(`${number} is a positive number`)
      : console.log(`${number} is a negative number`)
    number = -5

    number > 0
      ? console.log(`${number} is a positive number`)
      : console.log(`${number} is a negative number`)



Tags

Post a Comment

0Comments

Do leave your comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Ok, Go it!
To Top