Javascript WTF

A site where we can all share in those little WTF moments we enjoy with Javascript


27 December 2022

Assigning to and through literals and undefined

> 'abc'[1] = 'B' // this does NOT modify the 'abc' string literal (so far so good) but returns 'B'
> let B = 'abc'[1] = 'B' // so this does actually assign 'B' to B"
> undefined = 'B' // this does not change undefined (so far so good) but also returns 'B'
> let B = undefined = 'B' // so this does actually assign 'B' to B", through undefined

So not only can you assign something to undefined, but it is possible to assign a value through it.


04 May 2018

The Javascript Holy Trinity

Javascript Holy Trinity

JavaScript is as convoluted as Christian theology


04 May 2018

Arrays equal Strings?!

var a = [1,2,3];
var b = [1,2,3];
var c = '1,2,3';

a == c; // true
b == c; // true
a == b; // false

shuts laptop forever


03 November 2017

Appending a string to null treats it as a string

t = 'test'
undefined

t += 'ing'
"testing"

t=null
null

t += 'ing'
"nulling"

17 July 2017

Identical arrays are not equal?!

> ['a', 'b'] !== ['a', 'b']
true

> ['a', 'b'] != ['a', 'b']
true

> ['a', 'b'] == ['a', 'b']
false

Sure… Those are clearly not the same ;)


09 June 2017

WTF Strings?

> 'wtf' instanceof String
false

> typeof 'wtf'
'string'

> typeof String('wtf')
'string'

> String('wtf') === 'wtf'
true

09 June 2017

Undefined can be defined

> var t
< undefined

> t == undefined
< true      // Ok, that makes sense

> t = "Defined"
< "Defined" // Ok...
> var t
< undefined // ... so far, so good...
> t == undefined
< false     // ... What now?

09 June 2017

toString on numbers

You can cast floats to strings, but not integers.

> 3..toString()
"3"
> 3.toString()
Uncaught SyntaxError: Invalid or unexpected token

09 June 2017

Subtraction vs Addition vs Concatenation

String manipulation vs Maths

> '10' - 3
7

> '10' + 3
'103'
> '1' / '1'
1
> '1' * '1'
1
> '1' + '1'
'11'

09 June 2017

parseInt is strange!

parseInt(0.0000008, 10)
8

09 June 2017

Numbers get sorted alphabetically

> [10, 9, 8, 3, 2, 1, 0].sort()
[ 0, 1, 10, 2, 3, 8, 9 ]

09 June 2017

Null is and isn’t an object?

> typeof null // 'object'
> null instanceof Object // false

09 June 2017

Null Comparison

/* null comparation */

> 0 > null
false

> 0 >= null
true

> 0 == null
false

> 0 <= null
true

> 0 < null
false

> typeof null
'object'

> null instanceof Object
false

09 June 2017

NaN is a number

NaN is a number, but its’s not equal to itself!

> typeof NaN // 'Number'
> 123 === 123 // true
> NaN === NaN // false
> NaN == NaN // false

09 June 2017

Floating Point Maths

0.1 + 0.2 === 0.3 // false

(Clearly this is a normal floating point problem, but still!)


09 June 2017

Boolean Maths

/* boolean math */

> true + false
1

> true + true == true
false

09 June 2017

Array Coercion

[] == ![];     // true

Technically, empty array is falsey, just like a negated array. But thats not entirely obvious!


09 June 2017

Adding things up

> []+[] // ""
> []+{} // "[object Object]"
> {}+[] // 0
> {}+{} // [object Object][object Object]

Well those are some nice and consistent results!