JavaScript Operators
Overview
In programming, operators are special symbols that perform operations on operands (variables and values) to produce a result. Many operators perform arithmetic such as addition, subtraction, and multiplication. Others perform assignment, comparison, logical, string, and conditional operations.
Prepare
Understanding the types of operators is fundamental to programming.
Types of Operators
- Assignment operators assign values to variables.
Example Assignment Operators
Assignment operators Operator Description Example Same As =Assigns a value to a variable x = yx = y+=Addition assignment x += yx = x + y-=Subtraction assignment x -= yx = x - y*=Multiplication assignment x *= yx = x * y/=Division assignment x /= yx = x / y%=Remainder assignment x %= yx = x % y**=Exponentiation assignment x **= yx = x ** y&&=Logical AND assignment: assigns only if xis truthyx &&= yx && (x = y)||=Logical OR assignment: assigns only if xis falsyx ||= yx || (x = y)??=Nullish coalescing assignment: assigns only if xisnullorundefinedx ??= yx ?? (x = y) - Arithmetic operators perform arithmetic on numbers.
Example Arithmetic Operators
Arithmetic operators (with x = 10andy = 3)Operator Description Example Result +Addition x + y13-Subtraction x - y7*Multiplication x * y30/Division x / y3.3333333333333335%Remainder x % y1**Exponentiation x ** y1000++Increment (same as x = x + 1)x++xbecomes11--Decrement (same as x = x - 1)x--xbecomes9 - Comparison operators compare two values and return
trueorfalse. TutorialExample Comparison Operators
Comparison operators (with x = 5)Operator Description Example Result ==Loose equality: converts the types before comparing x == "5"true===Strict equality: value and type must both match x === "5"false!=Loose inequality x != "5"false!==Strict inequality x !== "5"true>Greater than x > 3true<Less than x < 3false>=Greater than or equal to x >= 5true<=Less than or equal to x <= 4falsePrefer the strict operators
===and!==. Loose equality converts types in ways that are easy to get wrong. - Logical operators (
&&,||,!) combine or negate conditions to create compound conditions.Example Logical Operators
Logical operators (with x = 5andy = 10)Operator Description Example Result &&AND: true only if both conditions are true x > 3 && y > 20false||OR: true if at least one condition is true x > 3 || y > 20true!NOT: reverses a condition !(x === y)true - String operators join text. The
+operator concatenates strings, as in"Hello, " + name. A template literal is often easier to read. - Conditional (ternary) operator – a compact alternative to an
if...elsestatement. Reference - Spread syntax (
...) – not technically an operator, but often listed with them. It expands an array or object into individual elements. Reference
References
- JavaScript Operators Reference – w3schools.com
- Expressions and Operators – MDN (in depth)
Check Your Understanding
- Using the article Basic math in JavaScript – numbers and operators, can you complete Test your skills: Math?
- Using the article Handling text – strings in JavaScript, can you complete Test your skills: Strings?