10 JAVASCRIPT INTRIGUING FACTS THAT WILL AMAZE YOU

Kingsley Ibe
5 min readJan 13, 2024
Javascript

INTRODUCTION

Javascript powers 98% of websites on the client side and is one of the oldest programming languages. In the ever-evolving world of web development, Javascript plays a pioneering role, in enhancing dynamic and interactive web pages. Despite its popularity, Javascript has intriguing facts that will shock even the most seasoned developer. In this article, I will walk you through 10 amazing discoveries of Javascript features and practices that will enlighten your understanding of the language. So, let’s dive into it:

#1. UNDEFINED IS NOT A RESERVED WORD: In Javascript, ‘undefined’ is not a keyword because it can be redefined and assigned a new value. Javascript has a default behaviour that doesn’t allow you to reassign values to keywords such as ‘if’, ‘else’, ‘for’, etc. ‘undefined’ is a global variable that represents a primitive value that can be reassigned a value. However, It’s recommended not to reassign values to undefined for code clarity and maintainability.

code sample for #1 UNDEFINED IS NOT A RESERVED WORD

#2. NaN (NOT A NUMBER) RETURNS A NUMBER: In JavaScript, ‘typeof NaN’ returns ‘number’ because NaN is considered a numeric data type within the IEEE 754 floating-point representation. In this standard, special values, such as NaN and Infinity, are part of the numeric type. For more straightforward output developers should always use ‘isNaN()’ to check for ‘NaN’ because the function checks specifically for the presence of ‘NaN’ values.

code sample for #2 NaN (NOT A NUMBER) RETURNS A NUMBER

#3. YOU CAN ALSO USE A SEMICOLON AT THE BEGINNING OF A LINE: NEVER DO THIS THOUGH. When you place a semicolon at the beginning of your code block, Javascript will run your code without errors; However, this is unconventional and may introduce readability issues. JavaScript interpreters automatically insert semicolons to terminate statements, and placing them at the beginning can disrupt this behaviour. I would recommend you follow standard coding conventions and only use semicolons where necessary to terminate statements.

code sample for #3 YOU CAN ALSO USE A SEMICOLON AT THE BEGINNING OF A LINE

#4. YOU CAN CONCATENATE A NUMBER AND A STRING WITHOUT PARSING IT: JavaScript allows you to concatenate a number and a string without explicitly converting the number to a string. By using the ‘+’ operator JavaScript will automatically convert the number to a string for concatenation.

code sample for #4 YOU CAN CONCATENATE A NUMBER AND A STRING WITHOUT PARSING IT

#5. NaN !== NaN: According to the IEEE 754 standard, NaN is considered unordered. This means that, from a logical standpoint, one NaN value is not considered equal to another NaN value. The reason behind this design is that NaN represents an undefined or unrepresentable value, and when two values are undefined or unrepresentable in the same way, they are not necessarily considered equal.

code sample for #5 NaN !== NaN

#6. COALESCING OPERATOR (??) PROVIDES A CONCISE WAY TO HANDLE NULL OR UNDEFINED VALUES: Javascript supports the nullish coalescing operator which is designed specifically for handling default values when dealing with “null” or “undefined”. It returns the right-hand operand when the left-hand operand is null or undefined. Otherwise, it returns the left-hand operand. It’s safe to use the ‘??’ operator in your everyday code to assign default values in “null” or “undefined” situations.

code sample for #6 NULLISH COALESCING OPERATOR (??) PROVIDES A CONCISE WAY TO HANDLE NULL OR UNDEFINED VALUES

#7. OBJECT.FREEZE() MAKES AN OBJECT IMMUTABLE: In Javascript, the “Object.freeze()” method is used to make mutable objects “immutable”. Any attempts to modify, add, or delete properties on immutable objects after freezing will not have any effect. This helps in creating objects with properties that cannot be changed once defined, ensuring immutability.

code sample for #7 OBJECT.FREEZE() MAKES AN OBJECT IMMUTABLE

#8. EVEN OBJECTS CAN BE EVALUATED AS TRUTHY OR FALSY, NOT JUST PRIMITIVE TYPES: In JavaScript, when a non-primitive value, such as an object, is used in a boolean context (e.g., in an if statement or as a condition), it undergoes a process called “truthy” or “falsy” coercion. This means that non-boolean values are automatically converted to a boolean for the condition evaluation. Only certain values, such as ‘null’, ‘undefined’, ‘0’, ‘NaN’, “ ” (empty string), and false, are considered falsy. This behavior can be useful when you want to check if an object exists or has been defined in your code.

code sample for #8 EVEN OBJECTS CAN BE EVALUATED AS TRUTHY OR FALSY, NOT JUST PRIMITIVE TYPES

#9. 0.1 + 0.2 !== 0.3: Again, this behaviour is related to the way floating-point numbers are represented in programming languages, not specific to JavaScript. The above expression will evaluate to true because “0.1 + 0.2” will output “0.30000000000000004”. It’s recommended to use methods like “toFixed()” or to compare floating-point numbers within a certain tolerance to handle such precision issues.

code sample for #9 0.1 + 0.2 !== 0.3

#10. NULL IS AN OBJECT BUT NOT AN INSTANCE OF ANY SPECIFIC OBJECT: In JavaScript, when “typeof null” is checked, it returns ‘object’. This result can be misleading, and it has led to the common misconception that null is an object. However, “null” is not an instance of any particular object type; it is a primitive value. The confusion arises from the fact that in the early days of JavaScript, the type of “null” was not properly distinguished. It was a mistake in the original design of the language that has been maintained for backward compatibility reasons.

code sample for #10 NULL IS AN OBJECT BUT NOT AN INSTANCE OF ANY SPECIFIC

Despite the ‘object’ result from ‘typeof null’, ‘null’ is not an instance of any specific object type, and treating it as such can lead to misunderstandings. It is advisable to check for ‘null’ explicitly using strict equality (‘===’) if needed, rather than relying on ‘typeof’ for this purpose.

Conclusion

I hope you are inspired by these highlighted 10 Javascript intriguing facts. To wrap them up, Javascript is more than a web development tool; it’s a realm of interesting features and hidden wonders. Whether you are a seasoned developer or just getting started, I would encourage you to explore more unknown facts about the language — Javascript.

Let’s keep the conversation going — share your thoughts! 🌐💡

--

--

Kingsley Ibe
Kingsley Ibe

Written by Kingsley Ibe

Software Engineer, Technical Writer, Chess Player

No responses yet