DEV Community

Cover image for ODD SIDE OF JAVASCRIPT😲!! 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭'𝐬 𝐐𝐮𝐢𝐫𝐤𝐲 𝐒𝐞𝐜𝐫𝐞𝐭𝐬!
Sahil Upadhyay
Sahil Upadhyay

Posted on

ODD SIDE OF JAVASCRIPT😲!! 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭'𝐬 𝐐𝐮𝐢𝐫𝐤𝐲 𝐒𝐞𝐜𝐫𝐞𝐭𝐬!

🚀 Ready for a jaw-dropping journey into the "Odd Side of JavaScript" 😲!? Brace yourself as we unravel the coding curiosities and share the surprising secrets that make JavaScript stand out. 💡💻 Get set for a fun ride through the unexpected twists and turns of this programming language! 🌐✨.

This post surely will be beneficial when you are in some where between solve the problem and search for the solution and Stack Overflow, ChatGPT might not be working as you want. Lets get started 🙌🌟..

  • Array's Equal Strings:--
var a = [0, 1, 2];
var b= [0, 1, 2];
var c = 0, 1, 2';
a = c; // true
b = c; // true
a = b; // false
Enter fullscreen mode Exit fullscreen mode

In JavaScript, arrays can seem similar to strings. If you have arrays like 'a' and 'b' with the same values, they are considered equal. However, if you introduce a string 'c' with the same values as 'a' and 'b', it won't be equal to them. So, when you say 'a = c' or 'b = c', it evaluates as true because they have the same values. But if you try 'a = b', it's false, even though they seem the same. This quirky behavior is a result of how JavaScript handles equality between arrays and strings, making it important to understand these nuances in your code.


  • Array's Are Not Equal:--
['a', 'b'] !== ['a', 'b'] // true
['a', 'b'] != ['a', 'b'] // true
['a', 'b'] == ['a', 'b'] // false
Enter fullscreen mode Exit fullscreen mode

In JavaScript, comparing arrays isn't as straightforward as it might seem. If you use '!==', it evaluates as true even if two arrays have the same values. Similarly, '!=' also returns true. Surprisingly, when you use '==', which might suggest equality, it gives a false result for arrays with the same content. This occurs because JavaScript compares arrays by reference, not content. So, even if arrays look the same, if they're separate instances in memory, they're considered not equal. This quirk highlights the importance of understanding how JavaScript handles array comparisons, ensuring precise and accurate coding.


  • String Is Not A String:--
typeof thecodecrumbs'; // "string"

'thecodecrumbs' instanceof String; // false
Enter fullscreen mode Exit fullscreen mode

Even though we use quotes for strings, they are considered primitive types, not objects. When you check the type with 'typeof', it correctly identifies it as a "string". However, if you try to use 'instanceof String', it surprisingly returns false. This discrepancy happens because primitive strings are not instances of the String object. They behave like strings but don't have the full set of properties and methods that come with the String object. This quirk is essential to note when working with strings in JavaScript, ensuring you use appropriate methods for the data type.


  • Null Comparision:--

typeof null;  // "object"

null instanceof Object;  // false
Enter fullscreen mode Exit fullscreen mode

In JavaScript, the 'typeof null' might seem confusing, as it returns "object," but null isn't an object. This behavior is a historical quirk. When JavaScript was created, 'null' was designed to represent the absence of a value, and using 'typeof' was a way to check for it. However, 'null instanceof Object' surprisingly evaluates as false. This discrepancy emphasizes that while 'typeof' suggests an object type, null is a distinct primitive value in JavaScript. It's a subtle nuance to be aware of when working with null values and object comparisons in your code.


  • NaN Is A Number:--
typeof NaN   // Number
123 === 123  //true
NaN === NaN  // false
Enter fullscreen mode Exit fullscreen mode

NaN (Not a Number) is indeed a type of number, as indicated by 'typeof NaN' returning "Number." However, a quirky aspect arises when you compare NaN values using '==='. While '123 === 123' rightly evaluates as true, 'NaN === NaN' surprisingly results in false. This behavior occurs because NaN represents an undefined or unrepresentable value, and according to the specifications, NaN is not equal to itself. This uniqueness in NaN comparisons highlights the need for careful handling when dealing with unexpected or undefined numerical values in JavaScript code.


  • Array's And Object:--
[][] // ""
[] + {} // "[object Object]"
{} + [] // 0
{} + {} // NaN
Enter fullscreen mode Exit fullscreen mode

Combining arrays and objects can lead to quirky results. When you use '[][]', it results in an empty string. Adding an empty array to an empty object '[] + {}' surprisingly gives "[object Object]". However, switching the order '{}' + [] results in 0, and combining two empty objects '{}' + {} yields NaN. These outcomes stem from JavaScript's complex type coercion rules, where the addition operator and object conversions can produce unexpected and diverse results. Understanding these nuances is crucial for precise coding when working with arrays and objects in JavaScript.


  • Boolean Maths:--
true + false //1
true + true == true  // false
Enter fullscreen mode Exit fullscreen mode

In JavaScript, boolean values undergo interesting behavior in mathematical operations. When you add 'true + false,' it surprisingly evaluates to 1. However, when you compare the result of 'true + true' to 'true,' it returns false. This quirk arises because JavaScript converts booleans to numeric values during mathematical operations, with 'true' being equal to 1 and 'false' to 0. So, 'true + true' becomes 2, which is not equal to 'true.' This illustrates the subtle and sometimes unexpected ways boolean values interact with numerical operations in JavaScript.


CONCLUSION

In the curious world of JavaScript oddities, we've uncovered fascinating quirks that make coding both intriguing and challenging. From arrays behaving unexpectedly to boolean's playing numerical games, understanding these nuances is key to writing precise and effective code. As you navigate the quirks of JavaScript, remember to follow me for more insights into the coding universe. Your thoughts and opinions are valuable, so don't forget to comment below! Let's continue this coding journey together, exploring the peculiar and delightful aspects of the language. Happy coding! 🚀💻 #JavaScriptAdventures #CodeQuirks #FollowForMore

Top comments (0)