Oh, JavaScript! π
Just when you think you've got it figured out, it throws you a curveball that leaves you scratching your head.
From its quirky type coercion to its peculiar equality comparisons, JavaScript's "weird parts" have been the source of both frustration and fascination for developers worldwide.
Let's take a journey through these quirks that aren't just bugs to be squashed, but features that give the language its unique flavor and power.
Why does in JavaScript?
Itβs due to which happens when the loose equality operator is used.
In the example below, is coerced to .
Since is equal to , the comparison returns !
An behaves similarly.
// Loose equality operator: (==)
0 == false // true
'' == false // true
In Javascript, when the comparison operators are used, is treated as zero.
Thus, these examples are :
// Comparison operators: >=, =<, >, <
null <= 0 // true
null >= 0 // true
0 <= 0 // true
0 >= 0 // true
The global property in JavaScript is a value representing
It represents the result of an operation that does not produce a well-defined numerical result.
It's behavior can be confusing as it is not equal to any other value in JavaScript, including itself!
NaN === NaN // false
Even though and have the same structure and content in the code below, they are two separate objects stored in
Primitive values like numbers, strings, and booleans are compared by whereas objects are compared by
let obj1 = { name: "Sarah", age: 30 }
let obj2 = { name: "Sarah", age: 30 }
obj1 == obj2 // false
obj1 === obj2 // false
In JavaScript, when we add two strings, they get concatenated to form a string.
But when we try to subtract one string from another, JavaScript attempts to
If either string cannot be converted to a number, the result is .
'ab' + 'cd' // 'abcd'
'ab' - 'cd'// NaN
Logical operators in JavaScript can be tricky π
The logical OR (||) operator returns the first truthy value it encounters or the last value if none are truthy.
The logical AND (&&) operator returns the first falsy value it encounters or the last value if none are falsy.
true || 'hello' // true
true && 'hello' // 'hello'
Array(3) == ",," // true
Yes, this code returns !
Letβs break it down:
in JavaScript creates a new array with a length of 3, but without initializing the elements.
This means it will look something like this:
[,,]
Now, we know that when comparing values of different types using the loose equality operator, JavaScript attempts to convert the operands to a common type before making the comparison.
In this case, the array is coerced to a leaving only the commas - - which means that our comparison is indeed !
By embracing JavaScript's idiosyncrasies, we can write more robust, efficient, and creative code.
After all, it's these very oddities that have allowed JavaScript to evolve from a simple scripting language into the powerhouse it is today, capable of running everything from complex web applications to server-side programs!
So the next time you encounter a JavaScript quirk, don't just scratch your head β dive in and explore! π
Congratulations for completing this resource! Mark it as completed to track your progress.