JavaScript Operators.

JavaScript Operators.

(30 Days Of JavaScript)

ยท

3 min read

An Expression is a special kind of statement that evaluates to a value. Every expression is composed of -

  • Operands: Represents the data.

  • Operator: Defines how the operands will be processed to produce a value.

Consider the following expressions 2+3. Here in the expression, 2 and 3 are operands and the symbol + (plus) is the operator. JavaScript supports the following types of operators.

Arithmetic Operators

arithmetic.JPG

Relational Operators Relational Operators test or define the kind of relationship between two entities. Relational Operators return a boolean value, i.e true or false.

Assume the value of A is 10 and B is 20.

Screenshot_20210528-142341[1].png

Logical Operators

Logical operators are used to combine two or more conditions. Logical operators, too return a Boolean Value. Assume the value of variable A is 10 and B is 20.

Screenshot_20210528-142734[1].png

Bitwise Operators

JavaScript supports the following bitwise operators. The following table summarizes JavaScript's bitwise operators.

bitwise.JPG

Assignment Operators

The following table summarizes assignment operators.

assignment.JPG

Note- The same logic applies to bitwise operators, so they will become <<=, >>=,>>=, &=, |= and ^=.

Miscellaneous Operators

Following are some of the miscellaneous operators.

The negation operator(-)

Changes the sign of a value. The following program is an example of the same.

var x = 4
var y = -x;
console.log("value of x:  ",x); //outputs 4
console.log("value of y:  ",y); //outputs -4

The following output is displayed on successful execution of the above program.

value of x:  4
value of y:  -4

String operators: Concatenation operator(+)

The + operator when applied to strings appends the second string to the first. The following program help to understand this concept.

var msg = "hello"+"world"
console.log(msg)

The following output is displayed on successful execution of the above program.

helloworld

The concatenation operation doesn't add a space between the strings. Multiple strings can be concatenated in a single statement.

Conditional Operator (?)

This operator is used to represent a conditional expression. The conditional operator is also sometimes referred to as the ternary operator. Following is the syntax

test ? expr1 : expr2

where, Test - refers to the conditional statement.

expr1 - value returned if the condition is true.

expr2 - value returned if the condition is false.

Example:

var num = -2
var result = num >
0 ?"positive" : "non-positive"
console.log(result)

Line 2 checks whether the value in the variable num is greater than zero. if num is set to a value greater than zero, it returns the string "positive" else a "non-positive" string is returned.

The following output is displayed on successful execution of the above program.

non-positive

Type Operators

typeof operator

It is a unary operator. This operator returns the data type of the operand. The following table lists the data types and the values returned by the typeof operator in JavaScript.

Screenshot_20210528-144856[1].png

The following example code displays the number as the output.

var num = 12
console.log(typeof num); //output: number

The following output is displayed on successful execution of the above code.

number

Did you find this article valuable?

Support Bode's blog by becoming a sponsor. Any amount is appreciated!