JavaScript Operators for Beginners : The Complete Reference


Posted on Fri, Aug 25, 2023 javascript web

Operators are essential building blocks in JavaScript. They allow you to perform operations on values and variables. This comprehensive guide covers all JavaScript operators in detail.

You'll learn about assignment, arithmetic, comparison, logical, string, and other operators.

The article demonstrates how to use each operator properly. By the end, you'll have a solid understanding of JavaScript operators to write cleaner and more efficient code.

What are JavaScript Operators ? 🀷🏼

JavaScript operators allow you to perform operations on values and variables. Operators are essential in JavaScript for manipulating data, executing code conditionally, and controlling program flow. Mastering the various operator types is key to writing efficient JavaScript code.

Importance of JavaScript Operators πŸ˜‡

Here are some key importance of JavaScript operators

🟰 Assignment Operators Operators like [=, +=, -=] : allow you to assign values to variables. This is crucial for storing data and updating values in your code.

πŸ‘ Arithmetic Operators Operators like [+, -, *, /, %] : allow you to perform basic math in JavaScript. You can calculate values, increment/decrement numbers, find remainders, etc. Math is key for many programming tasks.

πŸ†š Comparison Operators Operators like [==, ===, >, <] : let you compare values and check for equality. This is vital for decision-making and controlling program flow based on conditions.

🧠 Logical Operators Logical operators like [&&, ||, !] : allow you to combine comparisons and conditions. This lets you create complex logic by testing multiple cases and conditions. Being able to execute precise logical operations is important.

JavaScript Operators Reference

πŸ’‘

The following section provides an in-depth overview of the different JavaScript operator types including assignment, comparison, arithmetic, bitwise, logical, and more. The syntax and usage of each operator is explained.

Arithmetic Operators πŸŽ›οΈ

let result = 7 - 4;
console.log(result); // Outputs: 3

let result = 3 * 4;
console.log(result); // Outputs: 12

let result = 8 / 4;
console.log(result); // Outputs: 2

let result = 8 % 3;
console.log(result); // Outputs: 2

let num = 4;
num++;
console.log(num); // Outputs: 5

let num = 4;
num--;
console.log(num); // Outputs: 3

let result = 3 ** 2;
console.log(result); // Outputs: 9

let result = -3;
console.log(result); // Outputs: -3

let result = +"3";
console.log(result); // Outputs: 3

Comparison Operators βš–οΈ

let isEqual = '5' == 5;
console.log(isEqual); // Outputs: true

let isNotEqual = 5 != '6';
console.log(isNotEqual); // Outputs: true

let isStrictlyEqual = '5' === 5;
console.log(isStrictlyEqual); // Outputs: false

let isStrictlyNotEqual = 5 !== '5';
console.log(isStrictlyNotEqual); // Outputs: true

let isGreater = 6 > 3;
console.log(isGreater); // Outputs: true

let isLess = 3 < 6;
console.log(isLess); // Outputs: true

let isGreaterOrEqual = 6 >= 3;
console.log(isGreaterOrEqual); // Outputs: true

let isLessOrEqual = 3 <= 6;
console.log(isLessOrEqual); // Outputs: true

Logical Operators πŸ’­

let result = (6 > 3) && (7 < 10);
console.log(result); // Outputs: true

let result = (6 < 3) || (7 < 10);
console.log(result); // Outputs: true

let result = !(6 > 3);
console.log(result); // Outputs: false

let name = null;
let displayName = name ?? "Guest";
console.log(displayName); // Outputs: "Guest"

Assignment Operators πŸš₯

let age = 25;
console.log(age); // Outputs: 25

let num = 5;
num += 3;
console.log(num); // Outputs: 8

let num = 8;
num -= 3;
console.log(num); // Outputs: 5

let num = 4;
num *= 3;
console.log(num); // Outputs: 12

let num = 12;
num /= 3;
console.log(num); // Outputs: 4

let num = 13;
num %= 5;
console.log(num); // Outputs: 3

let num = 3;
num **= 3;
console.log(num); // Outputs: 27

let num = -5;
num >>>= 1;
console.log(num); // Outputs a large number due to unsigned shift

let num = 5; // Binary: 101
num &= 3;   // Binary: 011
console.log(num); // Outputs: 1, Binary: 001

let num = 5; // Binary: 101
num ^= 3;   // Binary: 011
console.log(num); // Outputs: 6, Binary: 110

let num = 5; // Binary: 101
num |= 3;   // Binary: 011
console.log(num); // Outputs: 7, Binary: 111

let value = "Hello";
value &&= "World";
console.log(value); // Outputs: "World"

let value = "";
value ||= "World";
console.log(value); // Outputs: "World"

let name = null;
name ??= "Guest";
console.log(name); // Outputs: "Guest"

Bitwise and Bit Shift Operators πŸ₯Ά

let result = 5 & 3; // 101 & 011
console.log(result); // Outputs: 1

let result = 5 | 3; // 101 | 011
console.log(result); // Outputs: 7

let result = 5 ^ 3; // 101 ^ 011
console.log(result); // Outputs: 6

let result = ~5; // ~101
console.log(result); // Outputs: -6

let result = 5 << 1; // 101 << 1
console.log(result); // Outputs: 10

let result = 5 >> 1; // 101 >> 1
console.log(result); // Outputs: 2

let result = -5 >>> 1;
console.log(result); // Outputs a large number due to unsigned shift

Relational Operators πŸͺ

let obj = {key: "value"};
console.log("key" in obj); // Outputs: true

let arr = [];
console.log(arr instanceof Array); // Outputs: true

Conditional (Ternary) Operator πŸ₯Έ

let age = 18;
let status = age >= 18 ? "adult" : "minor";
console.log(status); // Outputs: "adult"

Spread and Rest Operators 🎴

let str = "Hello";
console.log(typeof str); // Outputs: "string"

let date = new Date();
console.log(date instanceof Date); // Outputs: true

let arr = [1, 2, 3];
let newArr = [...arr, 4, 5];
console.log(newArr); // Outputs: [1, 2, 3, 4, 5]

function collect(...args) {
  console.log(args);
}
collect(1, 2, 3); // Outputs: [1, 2, 3]

Destructuring Assignment Operators πŸ’’

let [a, b, ...rest] = [1, 2, 3, 4, 5];
console.log(a, b, rest); // Outputs: 1 2 [3, 4, 5]

let {name, age, ...details} = {name: "John", age: 25, country: "US"};
console.log(name, age, details); // Outputs: John 25 {country: "US"}Other Operators

Other Operators πŸͺ„

let x = (5, 6);
console.log(x); // Outputs: 6

let obj = {name: "John"};
delete obj.name;
console.log(obj); // Outputs: {}

function Person(name) {
  this.name = name;
}

let person = new Person("John");
console.log(person.name); // Outputs: "John"

function Person(name) {
  this.name = name;
  this.sayName = function() {
    console.log(this.name);
  }
}

let person = new Person("John");
person.sayName(); // Outputs: "John"

class Child extends Parent {
  constructor() {
    super();
  }
}

console.log(void 0); // Outputs: undefined

async function fetchData() {
  let data = await fetch("https://api.example.com/data");
  return data.json();
}

function* generatorFunc() {
  yield 1;
  yield 2;
  return 3;
}

function* generatorA() {
  yield 1;
  yield* generatorB();
}

function* generatorB() {
  yield 2;
  yield 3;
}

πŸ”₯

That’s it! We have successfully covered the comprehensive operators in JavaScript!

Conclusion 🎈

JavaScript operators allow you to manipulate values and execute code in a variety of ways. Whether you need to do math, concatenate strings, compare values, or accomplish other tasks, there are operators available to suit your needs.

Learning how to properly use operators can help you write cleaner, more efficient JavaScript code. With practice, you'll be able to leverage operators to their full potential for any JavaScript project.