Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is = in JavaScript

The Basics: Equal Sign in JavaScript

In the world of JavaScript, the equal sign (=) is a fundamental operator that you will encounter often. It's used for a basic but crucial operation: assignment. Let's imagine that a variable in JavaScript is like a box. The equal sign (=) is like the action of placing something into this box.

let fruit = 'apple';

In the above example, fruit is our box and apple is what we're putting into the box. The equal sign (=) serves as the action that assigns the value apple to the variable fruit.

Understanding the Assignment Operation

The assignment operation in JavaScript is straightforward. The code let fruit = 'apple'; can be read as "fruit receives the value of apple". This operation can be done not just with strings like 'apple', but with all sorts of data types: numbers, booleans, objects, arrays, and so on.

let number = 10;
let isRaining = false;
let person = {name: 'John', age: 30};
let fruits = ['apple', 'banana', 'cherry'];

Variations of the Equal Sign

JavaScript is a language full of nuances, and the equal sign is no exception. Besides the simple assignment (=), we also have the comparison (==), the strict comparison (===), and compound assignment operators (+=, -=, *=, /=, etc.)

Comparison (==)

The double equal sign (==) is a comparison operator. It checks if the values on both sides are equal after type coercion. Type coercion means that JavaScript tries to convert the types to match each other before comparing.

let number = '10';
if (number == 10) { // true, because '10' is coerced to 10 before comparison
    console.log('They are equal!');
}

Strict Comparison (===)

The triple equal sign (===), or strict comparison, is like the double equal sign but without type coercion. It checks if both the value and the type on both sides are equal.

let number = '10';
if (number === 10) { // false, because '10' (string) and 10 (number) are not the same type
    console.log('They are strictly equal!');
}

Compound Assignment Operators (+=, -=, *=, /=)

Compound assignment operators are a shorthand way to update the value of a variable based on its current value.

let number = 10;
number += 5; // equivalent to number = number + 5; number is now 15
number -= 3; // equivalent to number = number - 3; number is now 12
number *= 2; // equivalent to number = number * 2; number is now 24
number /= 4; // equivalent to number = number / 4; number is now 6

Conclusion

In the grand scheme of coding with JavaScript, understanding the equal sign (=) and its variations is akin to a chef mastering the use of a knife. It's a basic tool, yes, but it's versatile and fundamental to the creation process.

As you continue your programming journey, remember that different situations call for different tools. Just as a chef chooses a different knife depending on whether they are filleting a fish or chopping vegetables, you'll need to decide whether to use =, ==, ===, or a compound operator based on your specific task. Keep practicing, keep experimenting, and before you know it, wielding these operators will become second nature to you. Happy coding!