rhondamuse.com

Unveiling 11 Quirky JavaScript Facts for Enthusiasts

Written on

Chapter 1: Hidden Secrets of JavaScript

Dive into the lesser-known aspects of JavaScript that can astonish both novices and experienced developers alike.

This paragraph will result in an indented block of text, typically used for quoting other text.

Section 1.1: The Peculiar Nature of JavaScript

The world of JavaScript is filled with surprises. For instance, when executing the following code, you may find the output quite unexpected:

('b' + 'a' + + 'a' + 'a').toLowerCase();

Surprisingly, this results in the string 'banana'.

JavaScript code output example

Another quirky example is:

"b" + "a" + Number("a") + "a"

This yields b a NaN a, highlighting JavaScript's unique handling of types.

Section 1.2: Understanding NaN

NaN, which stands for "Not a Number," is one of the most fascinating features of JavaScript. It represents a value that is neither finite nor infinite and cannot be compared to itself. For instance:

typeof NaN; // returns 'number'

To create a NaN, there are several methods, such as:

  • Dividing zero by zero: 0/0
  • Converting a non-numeric string: Number("a")
  • Performing calculations that result in an undefined outcome: (1/0) - (1/0)

Notably, comparing NaN to another NaN results in false:

NaN === NaN; // false

Always remember, NaN is neither finite nor infinite.

Chapter 2: JavaScript's Unique Traits

The first video titled "470 Adventure Time Facts You Should Know | Channel Frederator" delves into intriguing aspects of popular culture that can captivate audiences. This can be a great way to enhance your JavaScript knowledge while being entertained!

Section 2.1: The Quirk of Floating Point Arithmetic

In JavaScript, the addition of floating-point numbers can lead to unexpected results:

var a = 0.1;

var b = 0.2;

var c = a + b;

alert(c); // Outputs 0.30000000000000004

This peculiarity suggests that for precise calculations, especially in fields like cryptocurrency, alternatives to JavaScript may be necessary.

The second video titled "Build a Penguin - Step 36 | Learn CSS & HTML | FreeCodeCamp" is an excellent resource for those looking to expand their web development skills.

Section 2.2: The Mystery of Undefined

Interestingly, the undefined type can be manipulated, as seen here:

undefined = "I'm not undefined!";

var foo;

alert(undefined == foo); // true

Keep in mind that undefined is not a function.

Section 2.3: The Creation of JavaScript

JavaScript was developed in just ten days back in May 1995 by Brendan Eich at Netscape, which later became part of Mozilla. Despite its rapid creation, it has become one of the most beloved programming languages worldwide.

Section 2.4: The Concept of Null and Zero

JavaScript recognizes two types of null values: null and undefined. Similarly, it differentiates between positive and negative zero:

+0 === -0; // true

This leads to intriguing outcomes when performing mathematical operations that involve division by zero.

Section 2.5: Loosely Typed Language

JavaScript's flexibility allows a variable to store different data types over time:

var foo = 5;

console.log(foo); // Outputs 5

foo = "Rax";

console.log(foo); // Outputs "Rax"

This loose typing can be both advantageous and challenging, depending on the context.

Section 2.6: Backward Compatibility

One of JavaScript's significant advantages is its backward compatibility. Code written decades ago will still function in modern environments.

Section 2.7: Security Limitations

For security reasons, JavaScript does not allow access to the file system on the client’s side. This restriction helps protect users from potential vulnerabilities.

Section 2.8: Common Criticisms of JavaScript

Despite its popularity, JavaScript has its critics. Common grievances include its lack of traditional class structures and the challenges posed by its quirks, such as floating-point arithmetic issues.

References:

  • Deep Sea Demo
  • Mozilla JavaScript Blog
  • Wikipedia JavaScript

© 2022 — Originally published on Medium by Rakshit Shah.

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Join our community Discord.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Unlocking the Secrets of Emotional Intelligence: A Guide to Empathy

Explore the traits of emotionally intelligent individuals and learn how to enhance your emotional intelligence for better relationships.

Understanding the Impact of an Abusive Childhood Experience

Discover the journey of realizing and healing from an abusive childhood while embracing a happier future.

Wearable Health Monitors: The Future of Personalized Wellness

Discover how wearable health monitors are transforming personal healthcare and empowering individuals to manage their health effectively.