javascript confusing variable syntax [duplicate] - javascript

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?

Related

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?

JavaScript object like String literal doesn't to set property [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 2 years ago.
In JavaScript everything is object like String literal, numbers, Object...
And properties can be added to objects dynamically also, but I am adding property to object which refer to string then it seems it is not working
Following is JS code which I am running
var aVar = "some string";
console.log(aVar);
aVar.name = 'raj';
console.log(aVar.name);
Output...
hi
some string
undefined

Strange behaviour in browser: var name converts everything to string [duplicate]

This question already has answers here:
What is the global variable called 'name' in javascript? [duplicate]
(2 answers)
Using the variable "name" doesn't work with a JS object
(4 answers)
What is the difference between "let" and "var"?
(39 answers)
Closed 5 years ago.
since today I believe in ghosts.
Can someone explain the following JS behaviour with Chrome (Version 59.0.3071.115) or Firefox (54.0 (64-bit)) ?
Why?
var name = 10;
console.log(typeof name); // string
console.log(name); // "10"
Same with let works:
let name = 10;
console.log(typeof name); // number
console.log(name); // 10
What I believe is, that window.name is used and it transforms any input to string. But then, why is var name not a local variable and hides the global variable?
Thank you.

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.

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

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?

Categories

Resources