Site icon ni18 Blog

JavaScript Operators

Have you ever wondered how code does stuff—like adding numbers or deciding if one value is bigger than another? In JavaScript, the magic happens with expressions and operators. They’re like the ingredients and tools in a recipe: expressions mix things together to get a result, and operators are the actions (like adding or comparing) that make it happen.

In this guide, we’ll break down what expressions and operators are, explore their types, and see how they work in JavaScript. Whether you’re new to coding or brushing up, you’ll leave knowing how to use these building blocks in your own projects. Ready? Let’s get started!


What Are Expressions in Programming?

Think of an expression as a mini-question that JavaScript answers with a value. It’s like asking, “What’s 5 + 3?” and getting “8” back. In programming, an expression is any chunk of code that resolves to something—could be a number, a word, or even a true/false answer.

Two Types of Expressions

Expressions come in two flavors:

  1. Pure Expressions (Just Calculate)
    These only figure out a value and don’t mess with anything else.
  1. Expressions with Side Effects (Do More)
    These compute a value and change something in your program.

Why Expressions Matter

Expressions are everywhere in code. They’re how you add numbers, assign values, or even decide if something’s true. Without them, your program would just sit there doing nothing!


What Are Operators in Programming?

Operators are the action heroes of expressions. They’re symbols—like +, -, or =—that tell JavaScript what to do with values. In 3 + 4, the + is the operator saying, “Add these together!”

Operators team up with values (called operands) to make expressions work. For example:

Let’s explore the main types of operators in JavaScript!


Types of Operators in JavaScript

JavaScript has a toolbox full of operators, each with a special job. Here’s a rundown of the big categories, with examples to make them crystal clear.

1. Assignment Operators: The Value Givers

These operators put values into variables—like handing out gifts.

  let x = 5; // x now holds 5
  console.log(x); // Output: 5

2. Arithmetic Operators: The Math Wizards

These handle all your math needs—addition, subtraction, and more.

  console.log(5 + 3); // Output: 8
  console.log("Hello " + "world"); // Output: "Hello world"
  console.log(10 - 4); // Output: 6
  console.log(2 * 3); // Output: 6
  console.log(15 / 3); // Output: 5
  console.log(10 % 3); // Output: 1 (3 goes into 10 three times, remainder 1)
  console.log(2 ** 3); // Output: 8 (2 × 2 × 2)
  let a = 5;
  a++;
  console.log(a); // Output: 6
  let b = 5;
  b--;
  console.log(b); // Output: 4

3. Comparison Operators: The Decision Makers

These compare two values and return true or false.

  console.log(5 == "5"); // Output: true
  console.log(5 === "5"); // Output: false
  console.log(5 != "6"); // Output: true
  console.log(5 !== "5"); // Output: true
  console.log(3 < 5); // Output: true
  console.log(7 >= 7); // Output: true

4. Logical Operators: The Logic Builders

These combine true/false values for bigger decisions.

  console.log(5 > 3 && 2 < 4); // Output: true
  console.log(5 < 3 || 2 < 4); // Output: true
  console.log(!true); // Output: false
  let x = null ?? "default"; // Output: "default"

5. String Operators: The Word Joiners

The + operator doubles as a string combiner (called concatenation).

  let greet = "Hi" + " there";
  console.log(greet); // Output: "Hi there"
  let text = "Hello";
  text += " world";
  console.log(text); // Output: "Hello world"

6. Conditional (Ternary) Operator: The Shortcut

The ternary operator (?:) is a quick if-else in one line.

  let age = 20;
  let canVote = age >= 18 ? "Yes" : "No";
  console.log(canVote); // Output: "Yes"

7. Bitwise Operators: The Binary Ninjas

These work on numbers as binary bits (0s and 1s)—great for low-level tasks.

  console.log(5 & 3); // Output: 1 (binary: 101 & 011 = 001)

8. Unary Operators: The Solo Acts

These work on just one value.

  console.log(+"3"); // Output: 3
  console.log(-5); // Output: -5
  console.log(typeof "hello"); // Output: "string"

9. Comma Operator: The Multi-Tasker

The comma (,) runs multiple expressions and returns the last one.

  let x = (1 + 2, 3 + 4);
  console.log(x); // Output: 7

Quick Reference Table

Operator TypeExamplesJob Description
Assignment=, +=Sets or updates values
Arithmetic+, -, *, /Math operations
Comparison==, ===, <Compares values
Logical&&, ||, !Builds true/false logic
String+, +=Joins text
Ternary?:Quick if-else decisions
Bitwise&, |Binary-level operations
Unary+, -, typeofWorks on one value

Real-World Examples

Example 1: Shopping Total

let price = 10;
let tax = 2;
let total = price + tax;
console.log(total); // Output: 12

Example 2: Voting Check

let age = 16;
let canVote = age >= 18 ? "Yes" : "No";
console.log(canVote); // Output: "No"

Example 3: Greeting Builder

let name = "Alex";
let message = "Hello, " + name + "!";
console.log(message); // Output: "Hello, Alex!"

Tips for Using Expressions and Operators

  1. Keep It Simple: Start with basic stuff like + or =.
  2. Use ===: It’s safer than == for comparisons.
  3. Test Your Code: Try small examples to see how operators behave.
  4. Practice: Build something—like a calculator—to get comfy.

Conclusion: Your Coding Toolkit

Expressions and operators are the heart of JavaScript. Expressions give you results, and operators make those results happen—whether it’s adding numbers, comparing values, or flipping logic. From x = 5 to 3 + 4, they’re the tools you’ll use every day.

Exit mobile version