JavaScript Fun Facts: Discovering the Quirky Side of Code

Vinay ChhabraVinay Chhabra
5 min read

Hey everyone! Ever thought about the fun side of JavaScript? Well, get ready because we’re about to uncover some cool and quirky facts about this awesome coding language!

JavaScript isn’t just about writing code; it’s full of surprises and interesting tidbits that’ll make you go, “Wow!” Whether you’re a pro coder or just starting out, these facts will bring a smile to your face and make learning about JavaScript a blast.

So, grab a drink, relax, and let’s explore the fun side of JavaScript together! Ready? Let’s dive in!

The Naming Conundrum: JavaScript was originally named Mocha, then briefly LiveScript, before finally settling on JavaScript. This decision was influenced by Java’s popularity at the time, despite JavaScript’s distinctiveness as a language.

The NaN Mystery: NaN stands for “Not a Number,” but here’s the twist: the type of NaN is actually a number according to JavaScript. It’s a peculiar quirk that often catches developers off guard.

// Example code
var result = 10 / "Hello";
console.log(result); // Output: NaN
console.log(typeof result); // Output: "number"

The Infamous Hoisting: JavaScript hoists variable and function declarations to the top of their containing scope during compilation. This can lead to unexpected behavior if not understood properly, earning it the title of one of JavaScript’s notorious quirks.

// Example code
console.log(myVar); // Output: undefined
var myVar = 10;

NaN === NaN: Surprisingly, NaN is not equal to NaN in JavaScript. This seemingly bizarre behavior is due to NaN being considered a unique value that doesn’t equal anything, not even itself.

// Example code
console.log(NaN === NaN); // Output: false

The Semisemicolon: JavaScript automatically inserts semicolons at the end of lines, a feature known as Automatic Semicolon Insertion (ASI). However, relying on ASI can sometimes lead to unexpected bugs, making the semicolon debate among developers a fun and ongoing discussion.

The Truthy and Falsy Saga: JavaScript’s loose equality comparison can result in some unexpected outcomes, especially when dealing with truthy and falsy values. Empty arrays, objects, and strings with zero length are considered truthy, adding to the language’s quirky behavior.

// Example code
// Empty array is considered truthy
if ([]) {
  console.log("Empty array is truthy");
} else {
  console.log("Empty array is falsy");
}

// output
// Empty array is truthy

// Empty object is considered truthy
if ({}) {
  console.log("Empty object is truthy");
} else {
  console.log("Empty object is falsy");
}
// output
// Empty object is truthy

// Empty string is considered falsy
if ('') {
  console.log("Empty string is truthy");
} else {
  console.log("Empty string is falsy");
}
// output
// Empty string is falsy

The Ternary Operator’s Ternary: Did you know you can nest ternary operators in JavaScript? While not recommended for readability’s sake, it’s a fun fact that showcases the language’s flexibility (though it might induce headaches for future readers).

// Example code
const x = 10;
const y = (x > 5) ? ((x < 15) ? "x is between 5 and 15" : "x is greater than or equal to 15") : "x is less than or equal to 5";

console.log(y);
// output
// x is between 5 and 15

The Lengthy Undefined: Despite being a primitive value in JavaScript, undefined is not a reserved keyword. This means it can be reassigned, leading to potential confusion and bugs if not used carefully.

// Example code
var undefined = "I'm not undefined anymore!";
console.log(undefined);
// Output
// I'm not undefined anymore!
  • We’re reassigning the variable undefined to a string value.

  • When we log undefined, it prints the new value we assigned to it, rather than the default undefined value.

This behavior can lead to confusion and bugs if developers inadvertently reassign the undefined variable, assuming it still holds its original meaning. Therefore, it's essential to use the undefined value carefully and avoid reassigning it in your code.

The Enigmatic Infinity: JavaScript has a global Infinity property representing positive infinity. But did you know you can also get negative infinity by dividing a negative number by zero?

// Example code
const negativeInfinity = -1 / 0;
console.log(negativeInfinity);

// output
// -Infinity
  • We’re dividing a negative number (-1) by zero (0).

  • Dividing any non-zero number by zero in JavaScript results in either positive or negative infinity, depending on the sign of the numerator.

  • Since we’re dividing a negative number by zero, we get negative infinity (-Infinity).

This behavior showcases JavaScript’s handling of infinity, where dividing by zero leads to positive or negative infinity, depending on the sign of the numerator.

The Easter Egg: If you’re a fan of hidden surprises, you might enjoy this quirky JavaScript Easter egg.

In your browser’s address bar, try typing “javascript: void(0);” (without quotes) and hit Enter. While you won’t see anything visibly happen on the webpage, if you check the browser’s console, you’ll notice that the expression void(0) has been executed, resulting in undefined.

Note: Please be aware that many modern browsers have security measures in place to prevent the execution of JavaScript entered directly into the address bar. As such, this Easter egg may not work in all browsers or may require disabling certain security settings.

These fun facts showcase the quirky, surprising, and sometimes baffling nature of JavaScript, making it a fascinating language to explore and experiment with.

#JavaScriptFun #QuirkyCode #CodingHumor #TechTrivia #JavaScriptFacts #CodeSurprises #DevLife #ProgrammingHumor #JavaScriptEasterEgg #CodeQuirks

0
Subscribe to my newsletter

Read articles from Vinay Chhabra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vinay Chhabra
Vinay Chhabra

I'm Vinay Chhabra, a Full-Stack Developer and Software Engineer specializing in React.js, Node.js, and Magento 2. On Hashnode, I share insights on tech trends, development strategies, and e-commerce solutions.