DEV Community

(Not) Everything in JavaScript is an Object

Daniel Li on June 26, 2017

This was originally published on The Brewing Press For those who just wants the answers, feel free to jump to the 'Summary' at the end. There a...
Collapse
 
lambshift profile image
Juan Pablo de la Torre

Well, actually:

/* This is a zero -> */ 0 /* it could be any number */
// The following defines a function into 0's prototype
.__proto__.pow = function (exponent) {
   return Math.pow(this, exponent)
}

2..pow(5)     // 32
4.5.pow(3)    // 91.125
1.1e2.pow(2)  // 12100
0b100.pow(.5) // 2 > square root
0x1b.pow(1/3) // 3 > cubic root
Enter fullscreen mode Exit fullscreen mode

All of that is valid JS (including the comments between 0 and that period).

Collapse
 
3612_me profile image
3612 Muñoz

🤯

Collapse
 
samuraiseoul profile image
Sophie The Lionhart • Edited

Great article.

Remember kiddos,

String("string") == "string";

but

String("string") !== "string";

Collapse
 
bl41r profile image
David Smith • Edited
String("string") == "string";
true
String("string") === "string";
true
String("string") != "string";
false
String("string") !== "string";
false
Enter fullscreen mode Exit fullscreen mode
Collapse
 
samuraiseoul profile image
Sophie The Lionhart

Hrmmm..... I had done basically the same thing in my console when I made my comment, but I had a different result that day. I honestly expected your to be the case but I got what I typed before. I must have had a typo or something. MB

Thread Thread
 
bl41r profile image
David Smith • Edited

I think you forgot the new keyword:

y = "string"
"string"
x = new String("string")
String {0: "s", 1: "t", 2: "r", 3: "i", 4: "n", 5: "g", length: 6, [[PrimitiveValue]]: "string"}
x === y
false
String("string") === "string"
true
new String("string") === "string"
false
typeof(x)
"object"
typeof(y)
"string"
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
samuraiseoul profile image
Sophie The Lionhart

That's most likely it. You're far more knowledgeable than me in this area it seems. Thanks for adding the clarity for future readers and teaching me something!

Collapse
 
rapasoft profile image
Pavol Rajzak

I am definitely bookmarking this! Thanks

Collapse
 
kozlown profile image
Nigel

Excelent article !! :)

Collapse
 
andy profile image
Andy Zhao (he/him)

Awesome, now I have a cheatsheet. 📝 Super helpful for when I practice some JS. Thanks!

Collapse
 
ben profile image
Ben Halpern

Super practical article, thanks a lot Daniel.