DEV Community

Cover image for JavaScript Operators Explained: A Beginnerโ€™s Guide to Arithmetic, Comparison, Logical & More
WISDOMUDO
WISDOMUDO

Posted on

JavaScript Operators Explained: A Beginnerโ€™s Guide to Arithmetic, Comparison, Logical & More

Introduction

In JavaScript, operators are symbols or keywords used to perform operations on values and variables. Whether you're working with numbers, checking conditions, or making comparisons, JavaScript operators help you manage logic and control flow effectively.

As a beginner, understanding how these operators work will help you write better, cleaner, and more effective code. Our goal with this guide is to make learning JavaScript operators easy, clear, and accessible for beginners.

In the article, we'll cover the following areas:

  1. Arithmetic Operators (+, - ,* , /, %)
  2. Assignment Operators (=, +=, -=, etc.)
  3. Comparison Operators (==, ===, !=, !==, <, >, <=, >=)
  4. Logical Operators (&&, ||, !)
  5. Ternary Operator (condition? value_if_true: value_if_false)
  6. Type Operators ('typeof', 'instanceof')

Letโ€™s now go through each type one by one.

Arithmetic Operators

These operators allow you to perform mathematical operations on numbers.

Addition (+): This operator is commonly used to add one value to another.

Example:

let output = 5 + 3; // output is 8
๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Subtraction (-): Subtracts one value from another.

Example:

let output = 5 - 3; // output is 2

๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Multiplication (*): Multiplies two values.

Example:

let output = 5 * 3; // output is 15
๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Division ( / ): Divides one value by another.

Example:

let output = 6 / 3; // output is 2
๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Modulus (%): Returns the leftover part of a division between two values.

Example:

let output = 5 % 3; // output is 2
๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Assignment Operators

These are used for setting or updating variable values. They can also perform operations during an assignment.

Simple Assignment (=): In JavaScript, the equals sign (=) is called the assignment operator.

Example:

let a = 5; // a is now 5
๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Addition Assignment Operator (+=): Updates the variable by adding another value to it.

Example:

a += 3; // a is now 8 (a = a + 3)

๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Subtraction Assignment Operator (-=): Subtracts a value from the existing value.

Example:

a -= 2; // a is now 6 (a = a - 2)

๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Comparison Operators

Comparison operators test values against each other and produce a true or false outcome.

Equality Operator (==): Checks if two values are equal (loosely).

Example:

5 == '5'; // returns true because it only compares values, not types
๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Strict Equality Operator (===): Checks if two values are equal and of the same type (strict comparison).

Example:

5 === '5'; // returns false because the types are different (number vs. string)
๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Inequality Operator (!=): Checks if two values are not equal (loosely).

Example:

5 != '5'; // returns false because it only compares values

๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Strict Inequality Operator (!==): Checks if two values are not equal or of the same type.

Example:

5 !== '5'; // returns true because they are of different types

๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

<, >, <=, >=: Checks if one value is less than, greater than, less than or equal to, or greater than or equal to another value.

Example:

5 < 10; // returns true
5 >= 10; // returns false

๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Logical Operators

In programming, logical operators are tools for making decisions using Boolean values such as true and false.

AND (&&): The && operator evaluates to true only when both conditions are true.

Example:

true && false; // returns false
๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

OR ( || ): With OR, only one of the conditions needs to be true for the whole statement to be true.

Example:

true || false; // returns true
๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

NOT (!): The Not operator inverts the Boolean value; !true becomes false.

Example:

!true; // returns false
๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Ternary Operator

This operator allows you to simplify if-else logic into a single line with three elements: condition, true result, and false result.

Example:

let age = 20;
let canVote = age >= 20 ? 'Yes': 'No'; // canVote will be 'Yes' because the condition is true

๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Type Operators

These operators are used to check types and instances.

typeof: Returns the type of a variable or value.

Example:

typeof 'Hello'; // returns 'string'
typeof 5; // returns 'number
๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

instanceof: checks whether a given object is of a specified class.

Example:

let person = new Person();
person instanceof Person; // returns true
๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: wisdomudo/javascript operators explained a beginners guide to arithmetic comparison logical more ico - Pertandingan Hari Ini

Conclusion

Operators in JavaScript are fundamental building blocks that help you perform actions on variables and data. Whether you're doing calculations, making comparisons, or testing conditions, mastering operators will make your code more powerful and efficient. Start practicing with these operators and soon youโ€™ll be able to apply them confidently in your projects!

You can reach out to me via LinkedIn

Top comments (0)