03 - JavaScript - Operators

03 - JavaScript - Operators

JavaScript operators are symbols or keywords used to perform various operations on one or more values, producing new values. There are several types of operators in JavaScript. Arithmetic operators perform mathematical operations like addition, subtraction, multiplication, division, and modulus. Comparison operators test if two values are equal or not, while logical operators are used to test if a condition is true or false. Assignment operators store values in variables. Other operators include bitwise, string, and conditional operators, which enable more complex operations. JavaScript operators are useful for performing a range of operations on values, from simple arithmetic to complex logical operations. Whether you need to manipulate binary data, strings or create conditional statements, there is an operator for the job. Overall, JavaScript operators are crucial for web development and programming, making it an essential tool for developers.

Types of JS Operators

The different types of JS operators are as follows -

  • Arithmetic Operators

  • Assignment Operators

  • Comparison Operators

  • String Operators

  • Logical Operators

  • Bitwise Operators

  • Ternary Operators

  • Type Operators

Arithmetic Operators

JavaScript arithmetic operators are symbols used to perform mathematical operations on one or more numeric values. They produce a new numeric value as a result. There are several types of arithmetic operators in JavaScript -

  1. Addition Operator (+): The addition operator is used to add two or more numeric values together. For example, 2 + 3 returns the value 5.

  2. Subtraction Operator (-): The subtraction operator is used to subtract one numeric value from another. For example, 5 - 2 returns the value 3.

  3. Multiplication Operator (*): The multiplication operator is used to multiply two or more numeric values together. For example, 2 * 3 returns the value 6.

  4. Division Operator (/): The division operator is used to divide one numeric value by another. For example, 6 / 2 returns the value 3.

  5. Modulus Operator (%): The modulus operator returns the remainder of a division operation. For example, 5 % 2 returns the value 1, because 2 goes into 5 twice with a remainder of 1.

  6. Increment Operator (++): The increment operator increases the value of a variable by 1. For example, let a = 5; a++; will increase the value of a to 6.

  7. Decrement Operator (--): The decrement operator decreases the value of a variable by 1. For example, let b = 3; b--; will decrease the value of b to 2.

  8. Exponentiation Operator (**): The exponentiation operator raises the first operand to the power of the second operand. For example, 2 ** 3 returns the value 8.

Overall, JavaScript arithmetic operators are powerful tools for performing mathematical operations in programming.

Assignment Operators

JavaScript assignment operators are used to assign a value to a variable and perform additional operations on the variable being assigned. Here are some examples of assignment operators in JavaScript -

  1. Basic Assignment (=): The basic assignment operator assigns the value on the right-hand side of the operator to the variable on the left-hand side. For example, let x = 5; assigns the value 5 to the variable x.

  2. Addition Assignment (+=): The addition assignment operator adds the value on the right-hand side of the operator to the variable on the left-hand side. For example, let y = 3; y += 2; is equivalent to y = y + 2; and assigns the value 5 to the variable y.

  3. Subtraction Assignment (-=): The subtraction assignment operator subtracts the value on the right-hand side of the operator from the variable on the left-hand side. For example, let z = 8; z -= 3; is equivalent to z = z - 3; and assigns the value 5 to the variable z.

  4. Multiplication Assignment (*=): The multiplication assignment operator multiplies the variable on the left-hand side by the value on the right-hand side of the operator. For example, let a = 2; a *= 5; is equivalent to a = a * 5; and assigns the value 10 to the variable a.

  5. Division Assignment (/=): The division assignment operator divides the variable on the left-hand side by the value on the right-hand side of the operator. For example, let b = 10; b /= 2; is equivalent to b = b / 2; and assigns the value 5 to the variable b.

  6. Modulus Assignment (%=): The modulus assignment operator divides the variable on the left-hand side by the value on the right-hand side of the operator and assigns the remainder to the variable. For example, let c = 13; c %= 5; is equivalent to c = c % 5; and assigns the value 3 to the variable c.

  7. Exponentiation Assignment (**=): The exponentiation assignment operator raises the variable on the left-hand side to the power of the value on the right-hand side and assigns the result to the variable. For example, let d = 2; d **= 3; is equivalent to d = d ** 3; and assigns the value 8 to the variable d.

Overall, JavaScript assignment operators are useful for modifying and updating variable values in programming.

Summarizing Up

JavaScript has arithmetic operators such as addition, subtraction, multiplication, division, modulus, and exponentiation. These operators are used to perform mathematical calculations on numeric values. Additionally, JavaScript has assignment operators such as basic assignments, addition assignments, subtraction assignments, multiplication assignments, division assignments, modulus assignments, and exponentiation assignments. These operators are used to assign values to variables and perform additional operations on the variable being assigned. Both arithmetic and assignment operators are essential for performing complex calculations and updating variable values in JavaScript programming.

Did you find this article valuable?

Support manas krishna jaiswal by becoming a sponsor. Any amount is appreciated!