JavaScript Operators

Use operators to calculate, compare, assign, and combine values.

  • Operators perform actions on values.
  • Expressions combine values and operators to produce a result.
  • They are used for calculations, comparisons, and decisions.

What Are Operators?

  • An operator is a symbol that performs an action on values.
  • Common actions include adding, comparing, and assigning values.
JavaScript
let total = 10 + 5;
  • 10 and 5 are operands.
  • + is the operator.
  • The result is 15.

Arithmetic Operators

  • + adds values.
  • - subtracts values.
  • * multiplies values.
  • / divides values.
  • % returns the remainder.
  • ** raises a number to a power.
  • ++ increases a value by 1.
  • -- decreases a value by 1.
JavaScript
let a = 10;
let b = 5;

console.log(a + b); // Addition
console.log(a - b); // Subtraction
console.log(a * b); // Multiplication
console.log(a / b); // Division
console.log(a % b); // Remainder
JavaScript
console.log(2 ** 3); // 8

let count = 5;
count++; // 6
count--; // 5

Assignment Operators

  • = assigns a value.
  • += adds a value and saves the result.
  • -= subtracts a value and saves the result.
  • *= multiplies by a value and saves the result.
  • /= divides by a value and saves the result.
JavaScript
let score = 100;
  • The value 100 is assigned to score.
JavaScript
let points = 50;

points += 10;

console.log(points);
  • The output is 60.
  • points += 10 is the short form shown below.
JavaScript
points = points + 10;

Comparison Operators

  • Comparison operators return true or false.
  • === checks equal value and equal type.
  • !== checks that values or types are different.
  • > and < compare greater and smaller values.
  • >= and <= include equality in the comparison.
JavaScript
let age = 18;

console.log(age >= 18);
  • The output is true.

Logical Operators

  • && returns true when both conditions are true.
  • || returns true when at least one condition is true.
  • ! changes true to false or false to true.
  • ?? returns the right value when the left value is null or undefined.
JavaScript
let age = 25;
let hasLicense = true;

console.log(age >= 18 && hasLicense);
  • The output is true because both conditions are true.
JavaScript
let nickname = null;

console.log(nickname ?? "Guest"); // Guest

String Operators

  • + joins strings.
  • += adds text to an existing string.
JavaScript
let fullName = "Asha" + " " + "Kumar";
let message = "Hello";
message += " World";

Conditional Operator

  • ? : is also called the ternary operator.
  • It returns one value when a condition is true and another when false.
JavaScript
let age = 20;
let status = age >= 18 ? "Adult" : "Minor";

Type Operator

  • typeof returns the type of a value.
JavaScript
console.log(typeof 42);      // number
console.log(typeof "Hello"); // string
console.log(typeof true);    // boolean

Optional Chaining

  • ?. safely accesses a property or calls a function.
  • ?. returns undefined when the value before it is null or undefined.
JavaScript
const user = { profile: { name: "Asha" } };

console.log(user.account?.email); // undefined
user.onLogin?.();

Operator Precedence

  • Higher-precedence operators run before lower-precedence operators.
  • Multiplication runs before addition.
  • Parentheses run first and make the intended order clear.
JavaScript
console.log(2 + 3 * 4);   // 14
console.log((2 + 3) * 4); // 20

Key Takeaways

  • Arithmetic operators perform calculations.
  • Assignment operators store and update values.
  • Comparison and logical operators help make decisions.
  • String and ternary operators keep common tasks concise.
  • Use parentheses when the order of operations may be unclear.
Digital AI Academy

DevBrainBox Portal is proudly developed and managed by

Digital AI Academy
About the academy