8 Must-Know JavaScript Constants for Better Coding

Follow on LinkedIn

Hey there, fellow coder! Today, we’re going to chat about some of the built-in constants in JavaScript. These might sound a bit technical, but don’t worry—I’ll break them down for you with examples so you can see how they work in real-world coding.

1. null: The Intentional Absence of Value

In JavaScript, null represents the intentional absence of any object value. Think of it like a placeholder, signaling that “there’s supposed to be something here, but right now, there’s nothing.”

let car = null;
console.log(car); // Output: null

Here, car is explicitly set to null, meaning it’s intentionally empty.

2. NaN: Not a Number, But Still a Number?

NaN stands for “Not-a-Number,” which might sound a bit strange at first. It’s a special value that JavaScript assigns when a mathematical operation doesn’t result in a valid number.

let result = Math.sqrt(-1);
console.log(result); // Output: NaN

Taking the square root of a negative number doesn’t make sense in the realm of real numbers, so JavaScript returns NaN.

3. Testing for NaN using isNaN()

You might wonder, “How do I check if something is NaN?” JavaScript provides the isNaN() function to help you out.

let value = "hello" / 2;
if (isNaN(value)) {
    console.log("This is not a number!"); // This will run
}

Here, dividing a string by a number results in NaN, and isNaN(value) confirms it.

4. undefined vs. null: What’s the Difference?

Both undefined and null represent “nothing” in JavaScript, but they’re used in different situations. undefined is the default value for variables that have been declared but not initialized, while null is explicitly set by the programmer.

let car;
console.log(car); // Output: undefined

car = null;
console.log(car); // Output: null

The first log shows undefined because we haven’t assigned a value to car yet. The second log shows null because we explicitly set car to null.

5. Infinity and -Infinity: Beyond the Limits

JavaScript represents very large or very small numbers with Infinity and -Infinity. These values occur when you perform calculations that exceed the number range JavaScript can handle.

console.log(1 / 0);  // Output: Infinity
console.log(-1 / 0); // Output: -Infinity

Dividing by zero doesn’t throw an error; instead, it gives you Infinity or -Infinity.

6. Number Constants: The Unsung Heroes

JavaScript provides several number-related constants that can be handy in various situations. These include Number.MAX_VALUE, Number.MIN_VALUE, and more.

console.log(Number.MAX_VALUE); // Output: 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // Output: 5e-324

These constants represent the largest and smallest positive numbers that JavaScript can handle.

7. Operations that Return NaN

Not all operations yield valid results. In fact, many can return NaN, especially when you try to force non-numeric values into mathematical operations.

let text = "hello" - 5;
console.log(text); // Output: NaN

Subtracting a number from a string doesn’t make sense, so you get NaN.

8. Math Library Functions that Return NaN

Some functions in the Math library are designed to return NaN under specific conditions. For example, trying to calculate the square root of a negative number or taking the logarithm of a negative number will yield NaN.

console.log(Math.log(-1));   // Output: NaN
console.log(Math.sqrt(-9));  // Output: NaN

Both operations return NaN because they don’t make sense mathematically.

Conclusion

JavaScript’s built-in constants like null, NaN, and Infinity might seem simple, but they play crucial roles in ensuring our code behaves as expected. By understanding how and when to use them, you can write more robust and error-resistant programs.

That’s it for today’s journey into JavaScript’s constants! I hope you found this guide helpful. If you have any questions or comments, feel free to drop them below. Happy coding!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

×