JavaScript Operators
JavaScript Operators
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Conditional Operators
Type Operators
-= x -= y x=x-y x=5
*= x *= y x=x*y x = 50
/= x /= y x=x/y x=2
%= x %= y x=x%y x=0
: x: 45 size.x = 45 x = 45
ADVERTISEMENT
Given that t1 = "Good ", t2 = "Morning", and t3 = "", the table below
explains the operators:
Oper Example t1 t2 t3
Comparison Operators
Comparison operators are used in logical statements to determine equality
or difference between variables or values.
== equal to x == 8 false
== equal to x == 5 true
Logical Operators
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:
Example
let name = null;
let text = "missing";
let result = name ?? text;
Example
// Create an object:
const car = {type:"Fiat", model:"500", color:"white"};
// Ask for car name:
document.getElementById("demo").innerHTML = car?.name;
Note
The table above uses 4 bits unsigned number. Since JavaScript uses 32-bit
signed numbers, ~ 5 will not return 10. It will return -6.
~00000000000000000000000000000101 (~5)
will return
11111111111111111111111111111010 (-6)
Example
typeof "John" // Returns string
typeof 3.14 // Returns number
Please observe:
Example
typeof "John"
typeof 3.14
typeof NaN
typeof false
typeof [1, 2, 3, 4]
typeof {name:'John', age:34}
typeof new Date()
typeof function () {}
typeof myCar
typeof null
Note
You cannot use typeof to define if a JavaScript object is an array or a date.
Example
const person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
};
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.
Note
The delete operator should not be used on the properties of any predefined
JavaScript objects (Array, Boolean, Date, Function, Math, Number, RegExp,
and String).
Example
const q1 = ["Jan", "Feb", "Mar"];
const q2 = ["Apr", "May", "Jun"];
const q3 = ["Jul", "Aug", "Sep"];
const q4 = ["Oct", "Nov", "May"];
The ... operator can be used to expand an iterable into more arguments for
function calls:
Example
const numbers = [23,55,21,87,56];
let maxValue = Math.max(...numbers);
The in Operator
The in operator returns true if a property is in an object, otherwise false:
Object Example
const person = {firstName:"John", lastName:"Doe", age:50};
("firstName" in person);
("age" in person);
Note
You cannot use in to check for array content like ("Volvo" in cars).
Examples
const cars = ["Saab", "Volvo", "BMW"];
("Saab" in cars);
Predefined Objects
("PI" in Math);
("NaN" in Number);
("length" in String);
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>
Output
8 8 8 4
Output
30
Output
true
false
Output
false
true
Output
1
Output
Adult
Output
3
Output
6
6
Output
true
true
Output
1111111110111111111011111111100n
Output
Hello World
+ concatenates strings.
+= appends to an existing string.
12. JavaScript Chaining Operator (?.)
The optional chaining operator allows safe access to deeply nested
properties without throwing errors if the property doesn’t exist.
const obj = { name: "Aman", address: { city: "Delhi" } };
console.log(obj.address?.city);
console.log(obj.contact?.phone);
Output
Delhi
undefined