DEV Community

Ben Halpern
Ben Halpern Subscriber

Posted on

What is the oddest JavaScript behavior?

JavaScript is notorious for its inconsistencies in a lot of areas. It's also powerful and popular and has a lot going for it.

But can we poke fun at weird things? (whether or not the behavior is a good thing in general)

Let's start with an example:

+'a' resolves to NaN ("Not a Number") because it coerces a string to a number, while the character a cannot be parsed as a number

document.write(+'a');
To lowercase it becomes nan.

Adding NaN to "ba" turns NaN into the string "NaN" due to type…

Latest comments (85)

Collapse
 
damuz91 profile image
David Muñoz • Edited

This took hours of my life, i want them back!

"somestring".includes('') // true
Enter fullscreen mode Exit fullscreen mode
Collapse
 
brandonmack111 profile image
brandonmack111

I'd say definitely this:

(![] + [])[+[]] +
(![] + [])[+!+[]] +
([![]] + [][[]])[+!+[] + [+[]]] +
(![] + [])[!+[] + !+[]];
// -> 'fail'
Collapse
 
bergamin profile image
Guilherme Taffarel Bergamin

The weirdest thing for me in JS is the fact that you will only know if something works at runtime because there isn't any compilation.

This brings weird stuff like the undefined state. It is basically telling you that you are using something that doesn't exist. You know it should exist at that point, but because of some mistake somewhere you cannot identify because there is no compilation, you will have to check if that shit is undefined before checking if it is null before checking if it is empty.

Yes, I'm new to JS and I know you can test any variable as if they were Boolean to check for undefined and null, but I think that's not readable enough, so I would rather create a function with another name that checks for that.

Collapse
 
nateous profile image
Nate

Ben thanks for this! Over the years I've loved the quirks of JavaScript. It's a totally different way of coding, and I like it as an option. I'll stick to C# if I want true OO.

My favorite two have always been the guard clause (objNullRef && objNullRef.prop) and the default clause (objNullRef || fallBackObj).

Long live JavaScript!

Love these other comments, you guys are great!

Collapse
 
uriell profile image
Uriell • Edited
0.0.toString() !== "0.0"

this one left me wondering quite a while why my API was returning null:

JSON.stringify(NaN) // null
Collapse
 
cdanielsen profile image
Christian Danielsen
Collapse
 
fluffynuts profile image
Davyd McColl

I <3 Javascript, but: destroyallsoftware.com/talks/wat :D

Collapse
 
metacritical profile image
Pankaj Doharey • Edited

const a = Array(3).fill([null,null,null] )

// Is not the same as 

const b =  [[null,null, null],[null,null, null],[null,null, null]]

/* because if you try to fill an element in one array in the first example 
it fills all the array with the same element */

a[1][2] = "X";

//This results in the following.

/* 
=> 

[[null, null, "X"]
 [null, null, "X"]
 [null, null, "X"]] /*


/* Aah freaking javascript. */

Collapse
 
kenbellows profile image
Ken Bellows

Imo the implicit type conversion is a little weird, but even ignoring that, I think the point was the difference in behavior between '11' + 1, which converts the number to a string, and '11' - 1, which converts the string to a number. This discrepancy makes implicit type casting feel unpredictable and weird, at noon least to me

Collapse
 
antogarand profile image
Antony Garand