why javascript number is called a primitive when it has __proto__ [duplicate] - javascript

This question already has answers here:
Why do Javascript primitive values (e.g. name = "John") have properties and methods? [duplicate]
(2 answers)
javascript: do primitive strings have methods?
(2 answers)
Closed 6 years ago.
var num = 1;
typeof num; //prints number
console.log(num.__proto__); //prints Number
console.log(num.__proto__.__proto__); //prints Object
When variable num is behaving like a typical Object then why number is called primitive in javascript?

Related

javascript confusing variable syntax [duplicate]

This question already has answers here:
Javascript - Getting and setting properties on primitives implicitly creates object wrappers
(3 answers)
How is almost everything in Javascript an object?
(6 answers)
adding properties to primitive data types other than Array
(2 answers)
Closed 1 year ago.
I am new to JS, and JS syntax is definitely confusing to me as I'm used to other languages instead.
const x = 5;
x.yy = 3;
console.log(x.yy); // undefined
How is x.yy = 3; a valid syntax?
and why is x.yy undefined when x.yy =3; took in place?

Are Javascript objects always unique [duplicate]

This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
How to explain object references in ECMAScript terms?
(1 answer)
Javascript Object Identities
(6 answers)
Closed 1 year ago.
If I do:
const o1 = {};
const o2 = {};
is it guaranteed that o1===o2 is false?

Is it significant that samely-typed objects can never be considered 'the same' but samely-typed primitives can? [duplicate]

This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Why doesn't equality check work with arrays [duplicate]
(6 answers)
Object and primitive type equality
(5 answers)
Why isn't [1,2,3] equal to itself in Javascript? [duplicate]
(6 answers)
Closed 2 years ago.
Is it significant to an understanding of variables, memory, of primitive v.s. referential data-types that samely-typed objects can never be considered 'the same' but samely-typed primitives can?
console.log(5 === 5);
console.log({num: 1} === {num: 1});
//I'm aware that:
o = {num: 1}
o2 = o;
console.log(o === o2);//returns true
P.s. Genuine question, am hoping to get a better core understanding of JS as it seems to help so much later on to have this.

Accessing value of empty key in javascript object [duplicate]

This question already has answers here:
Should I use an empty property key?
(5 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
Javascript objects can accept an empty key with some value but how do we access the value of that empty key ?
var obj = {
"": "Name"
}
Try the following:
var obj = {"":"abc"};
console.log(obj[""]);

In Javascript, can I reference a key value pair from another key value pair? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Can a JavaScript object property refer to another property of the same object? [duplicate]
(2 answers)
How do I reference the same Object's properties during its creation? [duplicate]
(5 answers)
How can a JavaScript object refer to values in itself? [duplicate]
(8 answers)
Can I reference other properties during object declaration in JavaScript? [duplicate]
(7 answers)
Closed 5 years ago.
var fighters = ['johnjones', 'rondarousey', 'connormcgregor', 'chuckliddel', 'demetriusjohnson'];
var warriors = {
wrestlers: ['randysavage', 'hulkhogan', 'ultimatewarrior', 'jakethesnake', 'milliondollarman'],
stable: [fighters, warriors.wrestlers]
}
I believe I can reference fighters from stable, but can I reference wrestlers from stable? In other words, how do I reference a key value pair from a later key value pair within the warriors object. Thank you for any and all help!
You cannot do that until the variable warriors is initialized. The only way is to wait and assign in the next line:
var warriors = {
wrestlers: ['randysavage', 'hulkhogan', 'ultimatewarrior', 'jakethesnake', 'milliondollarman'],
}
warriors.stable = [fighters, warriors.wrestlers]
Or use some kind of weird initialization like the one described here. Or use function constructor.

Categories

Resources