DEV Community

Cover image for Java script Operators.
Chandru P
Chandru P

Posted on

Java script Operators.

JavaScript operators are special symbols or keywords used to perform calculations, combine values, modify variables, and execute logical checks in code. They act on values (operands) to produce a new result.


Core Categories of Operators

(https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imv3veq051oabmw6onn3.png)

Detailed Breakdown with Examples

1. Arithmetic Operators;-
Used for standard mathematical equations:

  • ++ (Addition): Adds two values or concatenates strings.

  • -(Subtraction): Subtracts the right value from the left.

    • (Multiplication): Multiplies two values.
  • /(Division): Divides the left value by the right.

  • %(Remainder/Modulus): Returns the division remainder.

  • ** (Exponentiation): Raises the base to the exponent power.

  • ++ (Increment): Increases an integer value by 1.

  • -- (Decrement): Decreases an integer value by 1.

javascriptlet total = 10 + 5 * 2; // Evaluates to 20 due to multiplication precedence

Enter fullscreen mode Exit fullscreen mode

2. Assignment Operators

Used to store values inside variables. Shorthand operators allow you to perform an operation and assign the result simultaneously:

`

  • +: Simple assignment (x = y)
  • +=: Add and assign (x = x + y)
  • -=: Subtract and assign (x = x - y)
  • *=: Multiply and assign (x = x * y)
  • /=: Divide and assign (x = x / y)`
let score = 10;
score += 5; // score is now 15

Enter fullscreen mode Exit fullscreen mode

3. Comparison Operators

Image (https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yltiuecr29enocmar01d.png)<br>

Always return a boolean value (true or false).

  • == (Abstract Equality): Compares values after performing type coercion.

  • === (Strict Equality): Compares both the value and the data type without conversion.

  • !=
    (Inequality): Checks if values are not equal.

  • !== (Strict Inequality): Checks if values or types are not equal.

  • >/<: Greater than / Less than.

  • >= / <= : Greater than or equal / Less than or equal.

 javascript

5 == "5";  // true (type coercion converts string to number)
5 === "5"; // false (different types: number vs string)

Enter fullscreen mode Exit fullscreen mode

4. Logical Operators

  • && (Logical AND): Returns trueonly if both operands are true.

-||(Logical OR): Returns true if at least one operand is true.

- ! (Logical NOT): Inverts the boolean value (true becomes false).

let accessAllowed = (age >= 18) && (hasTicket === true);

Enter fullscreen mode Exit fullscreen mode

1. What is the operator?

The operator is used to do calculations (i.e., to operate on the numeric values, strings, etc.,). Let us see strings in detail in a future blog.

Example,

let X = 5 + 10;
console.log(X); // 15
let Y = X + 10;
console.log(Y); // 25

Enter fullscreen mode Exit fullscreen mode

Operators in Javascript:-

Image (https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ywns0gve9a2mvoilci25.png)<br>

Arithmetic Operator:-

  • Arithmetic operators are used for doing arithmetic operations like addition, subtraction, multiplication, division, etc.,

  • Arithmetic Operators are +, -, , /, %, *, ++, --

  • To perform arithmetic operations, we need two values and they can be either numbers, variables, or expressions.

Image (https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rydc8ip3wcvbb2p11j71.png)

Let us see an example of basic arithmetic operations,

let a = 5;
let b = 6;
let add = a + b; // addition
console.log(add);// 11
let sub = a - b; // subtraction
console.log(sub);// -1
let mul=a * b; // multiplication
console.log(mul);// 30
let div=a / b; // division
console.log(div);// 0.8333333333333334
let modulo = a % b; // modulus
console.log(modulo);// 5

Enter fullscreen mode Exit fullscreen mode

What is ++ and -- operator?

  • ++ is known as Increment Operator.

  • is known as Decrement Operator.

Increment Operator:-
The increment operator is used to increment the value by 1.

Example;

let a = 5;
a++;
console.log(a);// 6

Enter fullscreen mode Exit fullscreen mode

Decrement Operator:-
The Decrement operator is used to decrement the value by 1.

Example;

let a = 5;
a –-;
console.log(a);// 4

In the above example, the value stored in the variable ais decremented by one value and so we got the decremented output as 4.

Assignment Operator:-

  • Assignment operators are used to assigning values to the Javascript Variables.

  • Assignment operators are =, +=, -=, =, /=, %=, *=

  • The ‘ = ’ operator is used to assign a value to a variable.

Example:-

let  container = 10;

Enter fullscreen mode Exit fullscreen mode

The ‘ += ’ operator is used to increment the value of a variable with a specified increment value.

Let us take an example to increment the variable what we will do? We will give as

let X = 10;
X = X+2;
console.log(X); // 12

Enter fullscreen mode Exit fullscreen mode

In the above example, we are incrementing the X value by 2. (Note:- the variable declared with the let keyword can be reassigned. So, we are reassigning the value of X to increment it).

Now, we can do the above same thing using the ‘ += ‘ operator, let's see how we can do it,

let X = 10;
X += 2;
console.log(X); // 12

Enter fullscreen mode Exit fullscreen mode

Comparison Operator:-

  • Comparison operators are used to compare two values and return an output with boolean values such as True or False.

  • Comparison operators are >, <, <=, >=, ==, !=

  • Comparison operators are used in Conditional Statements.

example,

let a = 10;
let b = 20;
let c = a>b;
console.log(c); //false
let d = a<b;
console.log(d); //true


Enter fullscreen mode Exit fullscreen mode

In the above example, first, we are checking a condition that a is greater than b according to our value. It checks and gives output as false and next we check that a is less than. It checks and gives output as true.

Identity Operator:-

  • === is called identity operator. It is strictly equal to(i.e., it checks or compares exactly whether they are equal or not and returns the output).

  • They check for their data type and also look-alike.

Logical Operator:-

Image (https://dev-to-uploads.s3.amazonaws.com/uploads/articles/660de808gin225rz00bw.png)<br>

  • The logical Operator evaluates the condition and returns output in True / False.

  • It is mostly used in Conditional Statements while comparing the two statements at the same time.

  • Logical operators are ||(OR), &&(AND), !(NOT).

Bitwise Operator:-

  • Bitwise operators are used for storing the memory size in bits and bytes.

  • Bitwise Operators are &, |, ~, ^, <<, >>, >>>.

  • We will see in-detail Bitwise operators with examples in future blogs after completing conditional statements.

  • In the next blog, we will explore Javascript Datatypes

Top comments (0)