DEV Community

Cover image for Operators and Expressions in JavaScript
arjun
arjun

Posted on

Operators and Expressions in JavaScript

Day 3: Operators and Expressions in JavaScript

Welcome to Day 3 of learning JavaScript! Today, we’ll explore operators and expressions—essential tools for performing calculations, making decisions, and writing meaningful logic in your programs.


What Are Operators?

Operators are special symbols or keywords that perform operations on values or variables. These operations can range from arithmetic calculations to logical decisions.


Types of Operators in JavaScript

1. Arithmetic Operators

Used for mathematical operations like addition, subtraction, multiplication, etc.

Operator Description Example Output
+ Addition 5 + 3 8
- Subtraction 10 - 6 4
* Multiplication 4 * 2 8
/ Division 12 / 4 3
% Modulus (Remainder) 10 % 3 1
** Exponentiation 2 ** 3 8

Example:

let num1 = 10;
let num2 = 3;
console.log(num1 + num2); // 13
console.log(num1 % num2); // 1
Enter fullscreen mode Exit fullscreen mode

2. Relational (Comparison) Operators

Used to compare two values and return a Boolean (true or false).

Operator Description Example Output
== Equal to 5 == "5" true
=== Strict equal to 5 === "5" false
!= Not equal to 5 != "5" false
!== Strict not equal 5 !== "5" true
< Less than 5 < 10 true
> Greater than 10 > 5 true
<= Less than or equal 5 <= 5 true
>= Greater than or equal 10 >= 5 true

Example:

let age = 20;
console.log(age >= 18); // true
console.log(age === "20"); // false
Enter fullscreen mode Exit fullscreen mode

3. Logical Operators

Combine multiple conditions or invert logic.

Operator Description Example Output
&& Logical AND true && false false
` ` Logical OR
! Logical NOT !true false

Example:

let isAdult = true;
let hasID = false;
console.log(isAdult && hasID); // false
console.log(isAdult || hasID); // true
Enter fullscreen mode Exit fullscreen mode

4. Assignment Operators

Used to assign values to variables.

Operator Description Example Output
= Assign x = 10 10
+= Add and assign x += 5 x = x + 5
-= Subtract and assign x -= 3 x = x - 3
*= Multiply and assign x *= 2 x = x * 2
/= Divide and assign x /= 2 x = x / 2
%= Modulus and assign x %= 3 x = x % 3

Example:

let points = 10;
points += 5; // 15
points *= 2; // 30
console.log(points);
Enter fullscreen mode Exit fullscreen mode

Expressions and Precedence

An expression is a piece of code that produces a value. For example, 5 + 3 is an expression, and its value is 8.

Operator Precedence

When multiple operators are used, JavaScript decides the order of execution based on precedence. Operators with higher precedence are executed first.

Operator Description Precedence
** Exponentiation 1
*, /, % Multiplication, Division, Modulus 2
+, - Addition, Subtraction 3
<, >, ==, != Comparison 4
&& Logical AND 5
` `

Example of Precedence:

let result = 10 + 5 * 2; // Multiplication happens first
console.log(result); // 20
Enter fullscreen mode Exit fullscreen mode

Use parentheses () to control precedence.

let corrected = (10 + 5) * 2; // Parentheses alter precedence
console.log(corrected); // 30
Enter fullscreen mode Exit fullscreen mode

Practice for Today

  1. Write a program to calculate the area of a rectangle using arithmetic operators.
  2. Compare two numbers using relational operators and log whether they are equal or one is greater.
  3. Create a simple program to check if a person is eligible to vote using logical operators.

Summary of Day 3

Today, we covered:

  1. Arithmetic Operators: For calculations.
  2. Relational Operators: For comparisons.
  3. Logical Operators: For combining conditions.
  4. Assignment Operators: For assigning and updating values.
  5. Expressions and Precedence: Understanding execution order.

Next Steps

In Day 4, we’ll learn about Control Flow in JavaScript, focusing on conditional statements and loops. Stay tuned for Dec 11, 2024!

Top comments (0)