- Home
- /
- Operators and Expressions
JavaScript Tutorial
IINTRODUCTION
JAVASCRIPT VARIABLES
JAVASCRIPT BASICS
JAVASCRIPT OBJECTS
DOM & BOM
OOPs
- Home
- /
- Operators and Expressions
Operators and Expressions
When you write code in JavaScript, you often need to do things like perform calculations, compare values, or assign values to variables. This is where operators come in. They are special symbols that carry out operations on one or more values (called operands).
For example, the +
operator adds two values together:
let total = 5 + 10; // total is 15
Or the =
operator assigns a value:
let name = "Arshyan";
Types of Operators
JavaScript has several types of operators you’ll use frequently:
Arithmetic Operators:
+
,-
,*
,/
,%
— used for math.Comparison Operators:
==
,!=
,>
,<
,>=
,<=
— used to compare values.Logical Operators:
&&
,||
,!
— used in conditions to combine or reverse logic.Assignment Operators:
=
,+=
,-=
,*=
,/=
— used to assign or update values.Ternary Operator:
condition ? valueIfTrue : valueIfFalse
— a shorthand for simple if/else.
What Are Expressions?
An expression is any piece of code that produces a value. You’ve already been writing them without even realizing it!
let x = 10;
let y = 20;
let result = x + y; // result is 30
Here, x + y
is an expression. It gets evaluated, and the result is stored in result
.
Operator Precedence
Now let’s talk about order of operations. Just like in math class, JavaScript follows certain rules for which operations to perform first.
For example:
let value = 10 + 5 * 3; // value is 25
Why? Because *
(multiplication) has higher precedence than +
(addition). So JavaScript does the multiplication first: 5 * 3 = 15
, then adds 10.
You can override this using parentheses:
let value = (10 + 5) * 3; // value is 45
Here, the addition happens first.
Final Thoughts
To sum up:
Operators let you do things like math, comparisons, and assignments.
Expressions combine values and operators to produce a result.
Operator precedence determines the order of operations—but parentheses give you control.
Mastering operators and expressions is a key step toward writing powerful and readable JavaScript code.