SBN

20 funny tricks of javascript that will make you laugh (and prob cry)

Javascript is kinda wild. It run everywhere, power the web, and sometimes power our headache too. It’s strong, fast (well, sometimes) but mostly weird in ways no one ask for. Here’s 20 funny lil tricks of js that probably gonna make you chuckle or facepalm.


1. [] + [] what even is this

console.log([] + []); // ""

Two empty arrays combine and poof… its a empty string. why though. 🤦


2. [] + {} vs {} + [] brain meltdown

console.log([] + {}); // "[object Object]"
console.log({} + []); // 0

Same symbol, diff order, diff result. js moodswings.


3. true + true = ??

console.log(true + true); // 2

yep booleans do math now. true just wanna be number 1, so 1+1=2. lol.


4. null is… an object???

console.log(typeof null); // "object"

Ask any dev. Been wrong since the 90s but they just never fix it.


5. NaN is not equal NaN

console.log(NaN === NaN); // false

Literally “not a number” isn’t equal to itself. makes no sense bro.


6. Math and strings doing whatever

console.log("5" - 2); // 3
console.log("5" + 2); // "52"

Minus make it number, plus make it string. why? just why??


7. parseInt being sneaky

console.log(parseInt("08")); // 8
console.log(parseInt("08", 8)); // 0

js sometimes think you using octal base. surprise math quiz.


8. float numbers are cursed

console.log(0.1 + 0.2 === 0.3); // false

Welcome to bank app nightmare. your cents just disappear.


9. array holes

let arr = [1, , 3];
console.log(arr.length); // 3
console.log(arr[1]); // undefined

You can literally skip an element and js just be like “ok fine”.


10. const objects not so constant

const person = { name: "JS" };
person.age = 25;
console.log(person); // { name: "JS", age: 25 }

Const dont mean constant. it mean “u cant reassign the box, but inside the box? go crazy”.


11. auto semicolons ruining ur day

function test() {
  return
  {
    value: 1
  }
}
console.log(test()); // undefined

Js be like “lemme help u with semicolons” and then destroy ur code.


12. double bang trick

console.log(!!"hello"); // true
console.log(!!0); // false

Two exclamations = boolean mode unlocked.


13. typeof infinity

console.log(typeof Infinity); // "number"

Infinity just a number now. math teachers crying somewhere in usa.


14. empty arrays are true

if ([]) {
  console.log("this runs!!");
}

Empty but still true. js logic.


15. keys are always strings

const obj = { 1: "one" };
console.log(obj[1]); // "one"
console.log(obj["1"]); // "one"

Numbers, strings, same same. js just dont care.


16. function is actually object

function hello() {}
hello.foo = "bar";
console.log(hello.foo); // "bar"

You can decorate functions like ur xmas tree. cute but useless.


17. arguments object

function weird(a, b) {
  console.log(arguments[0], arguments[1]);
}
weird(10); // 10 undefined

Old school hack, now just confusing. but still exist.


18. array sort without comparator

console.log([100, 25, 3].sort()); // [100, 25, 3]

Alphabet order by default, not numbers. 100 comes before 3. nice.


19. delete dont shrink arrays

let nums = [1, 2, 3];
delete nums[1];
console.log(nums); // [1, empty, 3

Delete just leave a hole. swiss cheese arrays.


20. typeof NaN

console.log(typeof NaN); // "number"

Not a number is… number. js is trolling us since forever.


final thoughts

Javascript is chaotic, weird, funny and also frustrating af. you hate it, but u cant live without it. next time you debug at 2am just remember: it’s not you, it’s js.

*** This is a Security Bloggers Network syndicated blog from SSOJet - Enterprise SSO & Identity Solutions authored by SSOJet - Enterprise SSO & Identity Solutions. Read the original post at: https://ssojet.com/blog/20-funny-tricks-of-javascript-that-will-make-you-laugh-and-prob-cry