What?! You called JavaScript Weird? Come on dude, I have using it for years, it is the best language I have ever met.
Let's have a coffee and see if we can ask some of these questions in our next interview, hehe.
Here we go:
> 0.1 + 0.2
0.30000000000000004
> 1 + “23” - 10
“113”
> null == undefined
true
> NaN == NaN
false
> typeof null
"object"
> typeof NaN
"number"
> ['1', '7', '11'].map(parseInt)
(3) [1, NaN, 3]
> function foo() {
return
{
foo: 'bar'
}
}
foo()
< undefined
> function bar() {
return {
foo: 'bar'
}
}
bar()
> {foo: "bar"}
> [] + []
""
> {} + {}
"[object Object][object Object]"
> [] + {}
"[object Object]"
> {} + []
0
> +!+[]
1
3 > 2
true
3 > 2 > 1
false
> function Test() {
}()
Uncaught SyntaxError: Unexpected token )
> var test = function() {}
undefined
References:
https://blog.mgechev.com/2013/02/22/javascript-the-weird-parts
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
Top comments (12)
This is a floating point rounding error and is definitely not exclusive to JavaScript. It happens with basically any language that implements floats.
I also don't think this one is language specific. It should evaluate the same nearly anywhere that follows a left to right execution order for same-precedence operators.
A lot of the other stuff is solid "weird language spec" though. I especially like this one:
Took me a moment to remember what it was doing there and figure out the trick. The fact that there are two separate bits of knowledge required is especially neat.
yes, you are right about these ones, I am including them to just showcase some 'weird' / interesting parts in JS. some of them are not actually
weird
, it is weird only when comparing with other languages.sorry my eye catching title is kind of misleading
I found that one interesting too. Took me some time.
I noticed that
works as expected.
parseint
is misinterpreting the second parameter somehow?Then I saw docs for Array.prototype.map.
If the callback accepts a second parameter, it passes the index of the element to it.
parseint
does accept two parameters, but of course, it doesn't expect an index in the third one.And sure enough, the output confirms my theory!
you are close, if you take a look at the api doc, you will fully understand it.
developer.mozilla.org/en-US/docs/W...
Yep! I'm aware. Pretty cool.
1 + "23" - 10 = 113
1 + "23" takes as string concatenation operation. The results is "123". Then it subtract 13.
Still not that clear how following things works.
[] + []
{} + {}
[] + {}
{} + []
+!+[]
I thought about
+!+[]
, it could be like these (I may be wrong)I think thats the solution. It's working on console.
Some of these have got to do with associativity, coercion and auto semicolon insertion.
3>2>1 this is based on left
right asociativity of the > operator 3>2 is evaluated first, which will be true and when we evaluate true >1 it will be false coz true is equal to 1 not greater.
After a return key if the interpreter does not find anything there will be an automatic semicolon insertion and returning nothing like that in the example will be undefined. All the code below the return key would be ignored.
But the language is interestingly weird! I think we can call it WeirdScript :)
i have seen this before here destroyallsoftware.com/talks/wat i guess.
o my freaking god