JavaScript Operator

JavaScript Operator

JavaScript Operator & Its Common Types

Featured on daily.dev

JavaScript is a popular object-oriented programming language for building an interactive web browser.

This article will expose you to one of the most frequently discussed topics in JavaScript, whether you're just starting out in programming or in JavaScript. The introduction of this article covers the fundamentals of the JavaScript Operator, its use and we'll discuss them following this sequence:

  • Operators.
  • Common Types of Operators.
  • Arithmetic Operators.
  • Mathematical Operators.
  • Compound Assignments Operators.
  • Increment/decrement Operators.
  • Comparison Operators.
  • Logical Operators.

Operators

Operators are used to perform actions on values and variables, such as assigning values to variables or storing values in variables. Take, for example, a basic mathematical expression: 7 + 3 = 10. In this case, the values are 7 and 3, and the operator is '+'.

Here are some of the most common operators in JavaScript.

Assignment Operators

The assignment operator is used to assign values to variables.

( = ), similar to a = 4

var a = 4;

// where a is the variable and 4 is the value

Mathematical Operators.

Arithmetic operations are carried out on values using mathematical operators, also referred to as arithmetic operators.

(- (subtraction)), + (addition), x (multiplication), and / (division), similar to a ** 4;

Consider

var a = 3 
var b = 5;
var c = a * b;
console.log(c);

Compound Assignment Operator

Compound Assignment operator is similar to the assignment operator but in this case the assigned value is either increased or decreased based on the appended operator.

The compound operators +=, -=, *=, and /= combine a mathematical operation with assignment, similar to a += 2 (same as a = a + 2).

Consider

// += (Add and Assign)
var a=5; 
a+=15; 
Now a = 20

// -= (Subtract and Assign)
var a=20;
a-=10;
Now a = 10

Increment/decrement Operators

++ (increment), -- (decrement), similar to a++ (similar to a = a + 1).

Consider

// increment
var a=10; 
a++; 
a = 11

//decrement

var a = 15;
a--;
a = 14;

Comparison Operators.

The JavaScript comparison operator compares the two values. The comparison operators are as follows:

== (loose-equals), === (strict-equals), != (loose not-equals), !== (strict not-equals), similar to a == b.

Consider

// is equal to ==
10 == 30 = false;

//identical
10 === 10 = true; (otherwise false)
  • Logical

    && (and), || (or), similar to a || b that selects either a or b.

Consider

// (&&) Logical AND 

(10==20 && 20==33) = false

// (||) or Logical OR
(10==20 || 20==33) = false

Conclusion

A keyword var is required in every program since it is the fundamental method for declaring (also known as creating) variables, i.e. keywords are used while assigning a value to variable or vice-versa.

Do you have any suggestions for JavaScript operators? Please drop in your comments!

Thank you.