An operator is a symbol that tells the computer to perform a specific mathematical or logical operation on one or more values (called operands). For example, the +
operator is used to add two numbers together, while the &&
operator is used to check if two conditions are both true.
There are many different types of operators in JavaScript, each with its own specific purpose. Some of the most common operators include:
-
Arithmetic operators: These operators are used to perform mathematical operations, such as addition (
+
), subtraction (-
), multiplication (*
), and division (/
). -
Comparison operators: These operators are used to compare two values, such as
>
(greater than),<
(less than), and==
(equal to). -
Logical operators: These operators are used to combine multiple conditions, such as
&&
(and),||
(or), and!
(not). -
Assignment operators: These operators are used to assign a value to a variable, such as
=
(equals),+=
(add and assign), and-=
(subtract and assign).
Here are some examples of how operators can be used in JavaScript:
// Arithmetic operators
let x = 5 + 3; // x is now 8
let y = 10 - 2; // y is now 8
let z = 4 * 2; // z is now 8
// Comparison operators
let a = 5 > 3; // a is now true
let b = 10 < 2; // b is now false
let c = 4 == 2; // c is now false
// Logical operators
let d = true && false; // d is now false
let e = true || false; // e is now true
let f = !true; // f is now false
// Assignment operators
let g = 5; // g is now 5
g += 3; // g is now 8
g -= 2; // g is now 6
Operators are an essential part of the JavaScript language, and they are used in almost every JavaScript program.
Top comments (0)