You are currently viewing JavaScript Cheat Sheet 10.9 : JavaScript Operators

JavaScript Cheat Sheet 10.9 : JavaScript Operators

Operators: A Comprehensive Guide for Beginners

In the world of programming, JavaScript is a versatile language that plays a crucial role in web development. It allows you to create dynamic and interactive web applications. To harness the power of JavaScript, you need to understand its fundamental building blocks. One of these building blocks is operators.

In this tutorial, we will explore the basics of JavaScript operators. We will cover various types of operators, provide code examples, and explain their functionalities. By the end of this tutorial, you will have a solid foundation in using operators to manipulate data and perform operations in JavaScript.

JavaScript Operators

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations in JavaScript. Here are some common arithmetic operators:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)

Example:

let x = 10;
let y = 3;

let sum = x + y;
let difference = x - y;
let product = x * y;
let quotient = x / y;
let remainder = x % y;

console.log(`Sum: ${sum}`);
console.log(`Difference: ${difference}`);
console.log(`Product: ${product}`);
console.log(`Quotient: ${quotient}`);
console.log(`Remainder: ${remainder}`);

Output:

Sum: 13
Difference: 7
Product: 30
Quotient: 3.3333333333333335
Remainder: 1

Explanation:

  • + adds two numbers.
  • - subtracts the second number from the first.
  • * multiplies two numbers.
  • / divides the first number by the second.
  • % gives the remainder when the first number is divided by the second.

2. Assignment Operators

Assignment operators are used to assign values to variables. The most basic assignment operator is the equal sign (=).

Example:

let x = 10;
let y = 5;

x += y; // Equivalent to x = x + y
console.log(`x: ${x}`);

Output:

x: 15

Explanation:

  • += adds the right operand to the left operand and assigns the result to the left operand.

3. Comparison Operators

Comparison operators are used to compare values. They return a boolean (true or false) result. Here are some common comparison operators:

  • Equal (==)
  • Not Equal (!=)
  • Strict Equal (===)
  • Strict Not Equal (!==)
  • Greater Than (>)
  • Less Than (<)
  • Greater Than or Equal (>=)
  • Less Than or Equal (<=)

Example:

let a = 5;
let b = "5";

console.log(a == b);    // true
console.log(a === b);   // false
console.log(a != b);    // false
console.log(a !== b);   // true
console.log(a > b);     // false
console.log(a < b);     // false
console.log(a >= b);    // true
console.log(a <= b);    // true

Output:

true
false
false
true
false
false
true
true

Explanation:

  • == and != compare values, and they perform type coercion if necessary.
  • === and !== compare values and types strictly.
  • >, <, >=, and <= compare numeric values.

4. Logical Operators

Logical operators are used to combine or manipulate boolean values. The common logical operators are:

  • Logical AND (&&)
  • Logical OR (||)
  • Logical NOT (!)

Example:

let isTrue = true;
let isFalse = false;

console.log(isTrue && isFalse);  // false
console.log(isTrue || isFalse);  // true
console.log(!isTrue);            // false

Output:

false
true
false

Explanation:

  • && returns true if both operands are true.
  • || returns true if at least one operand is true.
  • ! negates the boolean value.

5. Bitwise Operators

Bitwise operators are used to manipulate the binary representations of numbers. They are not commonly used in everyday programming.

Example:

let a = 5; // Binary: 101
let b = 3; // Binary: 011

console.log(a & b); // Bitwise AND (Binary: 001, Decimal: 1)
console.log(a | b); // Bitwise OR  (Binary: 111, Decimal: 7)
console.log(a ^ b); // Bitwise XOR (Binary: 110, Decimal: 6)
console.log(~a);    // Bitwise NOT (Binary: 11111111111111111111111111111010, Decimal: -6)
console.log(a << 1); // Bitwise Left Shift (Binary: 1010, Decimal: 10)
console.log(a >> 1); // Bitwise Right Shift (Binary: 10, Decimal: 2)

Explanation:

  • & performs a bitwise AND operation.
  • | performs a bitwise OR operation.
  • ^ performs a bitwise XOR operation.
  • ~ performs a bitwise NOT operation.
  • << shifts the bits to the left.
  • >> shifts the bits to the right.

6. Conditional (Ternary) Operator

The conditional operator (also known as the ternary operator) is a concise way to write conditional statements in JavaScript.

Example:

let age = 20;
let message = (age >= 18) ? "You are an adult" : "You are a minor";
console.log(message);

Output:

You are an adult

Explanation:

  • The ternary operator consists of a condition, a question mark (?), and two expressions separated by a colon (:). If the condition is true, the first expression is evaluated; otherwise, the second expression is evaluated.

7. Operator Precedence

Operator precedence determines the order in which operators are executed. In JavaScript, operators with higher precedence are executed first.

Example:

let result = 5 + 3 * 2;
console.log(result); // 11

Explanation:

  • In this example, the multiplication operator (*) has higher precedence than the addition operator (+), so it is executed first.

Conclusion

In this tutorial, we have covered the basics of JavaScript operators, including arithmetic, assignment, comparison, logical, bitwise, and conditional operators. Understanding and mastering these operators is essential for becoming proficient in JavaScript programming.

Operators are fundamental building blocks that allow you to manipulate data and control the flow of your programs. By applying the knowledge you’ve gained in this tutorial, you can start writing more complex and

dynamic JavaScript code.

We hope this tutorial has been helpful in your journey to learn JavaScript programming. Keep practicing, and you’ll soon become proficient in using operators to create interactive and dynamic web applications.

Leave a Reply