# Operators in JS

**Operators in Java Script**

An expression is a combination of operators operands that can be evaluated. It may also include function calls which return values.

**Examples**

x = 7.5 // a numeric literal

“Hello India!” // a string literal

false // a Boolean literal

{feet:10, inches:5} // an object literal

[2,5,6,3,5,7] // an array literal

v= m + n; // the variable v

tot // the variable tot

**ARITHMETIC OPERATORS**

These are used to perform arithmetic/mathematical operations like subtraction, division, multiplication etc. Arithmetic operators work on one or more numerical values (either literals or variables) and return a single numerical value. The basic arithmetic operators are:

Operator | Operator |

+ (Addition) | - (Subtraction) |

* (Multiplication) | / (Division) |

% (Modulus) |

Examples

var s = 10 + 20; // result: s=30

var h = 50 * 4; // result: h = 200

var d = 100 / 4; // result: d = 25

var r = 72 % 14; // result: r=2

**Increment and decrement operators**

These operators are used for increasing or decreasing the value of a variable by 1. Calculations performed using these operators are very fast.

++ Increment (By One)

-- Decrement (By One)

**Example**

var a = 15;

a++; // result: a = 16

var b = 20;

b—; // result: b = 19

**ASSIGNMENT OPERATORS**

It assigns the value of its right operand to its left operand. This operator is represented by equal sign(=).

**Example**

x = 100; // This statement assigns the value 100 to x.

JavaScript also supports shorthand operator for standard operations.

The shorthand operator with example :

Shorthand operator | Example | is equivalent to |

+= | a + = b | a = a + b |

-= | a - = b | a = a - b |

*= | a * = b | a = a * b |

/= | a / = b | a = a / b |

%= | a % = b | a = a % b |

**RELATIONAL (COMPARISON) OPERATORS**

Relational Operators are some symbols which return a Boolean value true or false after evaluating the condition. For example x > y; returns a value true is value of variable x is greater than variable y.

Basic JavaScript comparison operators are given in the table below :

Operator | Description | Example |

= = | is equal to | 4 = = 8 returns false |

! = | is not equal to | 4 ! = 8 returns true |

> | is greater than | 8 > 4 returns true |

< | is less than | 8 > 4 returns false |

< = | is less than or equal to | 8 < = 4 returns false |

> = | is greater than or equal to | 8 > = 4 returns true |

Relational operators are functional for strings as well. The comparison takes place in alphabetical order. This alphabetical order is based on ASCII number. For example :

Statement | Output |

“zero” < “one” | false |

“Zero” < “one” | true |

10 < 5 | false, numeric comparison. |

“10” < “5” | true, string comparison. |

“10” < 5 | false, numeric comparison; |

“Ten” < 5 | Error occurs, “Ten” can not be converted into a number |

**LOGICAL OPERATORS**

Logical operators are used for combining two or more conditions. JavaScript has following three logical operators :

Operator | Description with Example |

&& (AND) | returns true if both operands are true else it return false. |

| | (OR) | returns false if both operands are false else it returns true. |

! (NOT) | returns true if the operand is false and false if operand is true. |

CONCATENATION OPERATOR

The + operator concatenates two string operands. The + operator gives priority to string operands over numeric operands It works from left to right. The results depend on the order in which operations are performed.

For example :

Statement | Output |

“Good” + “Morning” | “GoodMorning” |

“5” + “10” | “510” |

“Lucky” + 7 | “Lucky7” |

4 + 7 + “Delhi” | “11Delhi” |

“Mumbai” + 0 +0+ 7 | “Mumbai007” |

Conditional Operator ( ? : )

The conditional operator is a special JavaScript operator that takes three operands. Hence, it is also called ternary operator. A conditional operator assigns a value to a variable based on the condition.

**Syntax**

`var_name = (condition) ? v_1 : v_2`

If (condition) is true, the value v_1 is assigned to the variable, otherwise, it assigns the value v_2 to the variable.

**For example**

`status = (age >= 18) ? “adult” : “minor”`

This statement assigns the value “adult” to the variable status if age is eighteen or more. Otherwise, it assigns the value “minor” to status.

**New Operator:**

new operator is used to create an instance and allocate memory to a user-defined or predefined object types.

**Syntax**

ObjectName = new objectType ( param1 [,param2] ...[,paramN])

**Example**

d = new Date(); // date assigns to object d

r = new rectangle(4, 5, 7, 8);

**Delete Operator**

The delete operator de-allocates (releases) the memory space that was allocated using the new operator by deleting an object, an object’s property or an element from an array.

**The syntax is**

delete object_name

delete object_name.property

delete array_name[index]

delete operator can be used to delete variables declared implicitly but not those declared with the var statement. The delete operator returns true if the operation is possible; it returns false if the operation is not possible.

**Qus. 1**: Which comparison operator is used to find equality of type as well as content between two variables?

- ==
- ===
- ====
- None of the above

**Qus. 2**: Exponential operator in JavaScript is:

- ^
- %
- **
- None of the above

**Qus. 3**: Identify the invalid statement about Angular JS Expressions

- written inside doublebr braces: {{expression }}
- written inside a directive: ng-bind="expression "
- return the result exactly where the expression is written
- Angular JS expressions are different from JavaScript expressions

**Qus. 4**: Which of the following can be used as a prefix for Directives ?

- data
- ng-
- Both (A) and (B)
- None of the above