Our Recommendation for You Search your Query, You can find easily. for example search by book name or course name or any other which is related to your education

label name

PHP

JS Operators

JavaScript Operators ReferenceJAVASCRIPT


JavaScript operators are used to assign values, compare values, perform arithmetic operations, and more.

JavaScript Arithmetic Operators

Arithmetic operators are used to perform arithmetic between variables and/or values.
Given that y = 5, the table below explains the arithmetic operators:
OperatorDescriptionExampleResult in yResult in xTry it
+Additionx = y + 2y = 5x = 7
-Subtractionx = y - 2y = 5x = 3
*Multiplicationx = y * 2y = 5x = 10
/Divisionx = y / 2y = 5x = 2.5
%Modulus (division remainder)x = y % 2y = 5x = 1
++Incrementx = ++yy = 6x = 6
x = y++y = 6x = 5
--Decrementx = --yy = 4x = 4
x = y--y = 4x = 5
For a tutorial about arithmetic operators, read our JavaScript Arithmetic Tutorial.

JavaScript Assignment Operators

Assignment operators are used to assign values to JavaScript variables.
Given that x = 10 and y = 5, the table below explains the assignment operators:
OperatorExampleSame AsResult in xTry it
=x = yx = yx = 5
+=x += yx = x + yx = 15
-=x -= yx = x - yx = 5
*=x *= yx = x * yx = 50
/=x /= yx = x / yx = 2
%=x %= yx = x % yx = 0
For a tutorial about assignment operators, read our JavaScript Assignment Tutorial.


JavaScript String OperatorsJAVASCRIPT

The + operator, and the += operator can also be used to concatenate (add) strings.
Given that text1 = "Good ", text2 = "Morning", and text3 = "", the table below explains the operators:
OperatorExampletext1text2text3Try it
+text3 = text1 + text2"Good ""Morning" "Good Morning"
+=text1 += text2"Good Morning""Morning"""

Comparison OperatorsJAVASCRIPT

Comparison operators are used in logical statements to determine equality or difference between variables or values.
Given that x = 5, the table below explains the comparison operators:
OperatorDescriptionComparingReturnsTry it
==equal tox == 8false
x == 5true
===equal value and equal typex === "5"false
x === 5true
!=not equalx != 8true
!==not equal value or not equal typex !== "5"true
x !== 5false
>greater thanx > 8false
<less thanx < 8true
>=greater than or equal tox >= 8false
<=less than or equal tox <= 8true
For a tutorial about comparison operators, read our JavaScript Comparisons Tutorial.

Conditional (Ternary) OperatorJAVASCRIPT

The conditional operator assigns a value to a variable based on a condition.
SyntaxExampleTry it
variablename = (condition) ? value1:value2voteable = (age < 18) ? "Too young":"Old enough";
Example explained: If the variable "age" is a value below 18, the value of the variable "voteable" will be "Too young", otherwise the value of voteable will be "Old enough".

Logical OperatorsJAVASCRIPT

Logical operators are used to determine the logic between variables or values.
Given that x = 6 and y = 3, the table below explains the logical operators:
OperatorDescriptionExampleTry it
&&and(x < 10 && y > 1) is true
||or(x === 5 || y === 5) is false
!not!(x === y) is true

JavaScript Bitwise OperatorsJAVASCRIPT

Bit operators work on 32 bits numbers. Any numeric operand in the operation is converted into a 32 bit number. The result is converted back to a JavaScript number.
OperatorDescriptionExampleSame asResultDecimal
&ANDx = 5 & 10101 & 00010001 1
|ORx = 5 | 10101 | 00010101 5
~NOTx = ~ 5 ~01011010 10
^XORx = 5 ^ 10101 ^ 00010100 4
<<Left shiftx = 5 << 10101 << 11010 10
>>Right shiftx = 5 >> 10101 >> 10010  2
The examples above uses 4 bits unsigned examples. But JavaScript uses 32-bit signed numbers.

Because of this, in JavaScript, ~ 5 will not return 10. It will return -6.

~00000000000000000000000000000101 will return 11111111111111111111111111111010

The typeof OperatorJAVASCRIPT

The typeof operator returns the type of a variable, object, function or expression:

Example

typeof "John"                 // Returns string typeof 3.14                   // Returns numbertypeof NaN                    // Returns numbertypeof false                 // Returns booleantypeof [1, 2, 3, 4]           // Returns objecttypeof {name:'John', age:34// Returns objecttypeof new Date()             // Returns objecttypeof function () {}         // Returns functiontypeof myCar                  // Returns undefined (if myCar is not declared)typeof null                   // Returns object
Please observe:
  • The data type of NaN is number
  • The data type of an array is object
  • The data type of a date is object
  • The data type of null is object
  • The data type of an undefined variable is undefined
You cannot use typeof to define if a JavaScript object is an array (or a date).

The delete OperatorJAVASCRIPT

The delete operator deletes a property from an object:

Example

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
delete person.age;   // or delete person["age"];
The delete operator deletes both the value of the property and the property itself.
After deletion, the property cannot be used before it is added back again.
The delete operator is designed to be used on object properties. It has no effect on variables or functions.
Note: The delete operator should not be used on predefined JavaScript object properties. It can crash your application.

The in OperatorJAVASCRIPT

The in operator returns true if the specified property is in the specified object, otherwise false:

Example

// Arraysvar cars = ["Saab", "Volvo", "BMW"];
"Saab" in cars          // Returns false (specify the index number instead of value)0 in cars               // Returns true1 in cars               // Returns true4 in cars               // Returns false (does not exist)"length" in cars        // Returns true  (length is an Array property)
// Objects var person = {firstName:"John", lastName:"Doe", age:50};
"firstName" in person   // Returns true"age" in person         // Returns true
// Predefined objects"PI" in Math            // Returns true"NaN" in Number         // Returns true"length" in String      // Returns true

The instanceof OperatorJAVASCRIPT

The instanceof operator returns true if the specified object is an instance of the specified object:

Example

var cars = ["Saab", "Volvo", "BMW"];

cars instanceof Array;          // Returns truecars instanceof Object;         // Returns truecars instanceof String;         // Returns falsecars instanceof Number;         // Returns false

The void OperatorJAVASCRIPT

The void operator evaluates an expression and returns undefined. This operator is often used to obtain the undefined primitive value, using "void(0)" (useful when evaluating an expression without using the return value).

Example

<a href="javascript:void(0);">
  Useless link
</a>

<a href="javascript:void(document.body.style.backgroundColor='red');">
  Click me to change the background color of body to red
</a>