Why do these two dynamic property assignments collapse into a single value?
up vote
2
down vote
favorite
I may be asking a basic question but I don't know how to clear such a confusion through google search
this below code works fine and count words, so I was trying to understand further
var words = chunk.trim().split(' ');
var counts = {};
// Count words
words.forEach(function (word) {
word = word.trim();
if (word.length) {
if (!counts[word]) {
counts[word] = 0;
}
counts[word]++;
}
});
then why not this below gives me 1,2 . I am expecting 1,2 and its giving me 2,2
var a = function(){
var a, b;
var obj = {}
obj[a]=1;
obj[b]=2;
console.log(obj[a]);
console.log(obj[b]);
}
var res = a();
javascript
add a comment |
up vote
2
down vote
favorite
I may be asking a basic question but I don't know how to clear such a confusion through google search
this below code works fine and count words, so I was trying to understand further
var words = chunk.trim().split(' ');
var counts = {};
// Count words
words.forEach(function (word) {
word = word.trim();
if (word.length) {
if (!counts[word]) {
counts[word] = 0;
}
counts[word]++;
}
});
then why not this below gives me 1,2 . I am expecting 1,2 and its giving me 2,2
var a = function(){
var a, b;
var obj = {}
obj[a]=1;
obj[b]=2;
console.log(obj[a]);
console.log(obj[b]);
}
var res = a();
javascript
1
Bothaandbareundefined, so the only property you're setting onobjhas the name "undefined", so it gets the last value you set. All object properties are basically strings (except Symbols), so whatever you give for a property gets converted to a string.
– user1106925
Apr 20 '17 at 16:58
Various ways to test this would be toconsole.log()the values ofaandb, and to log the result ofObject.keys(obj)to see what keys you gave it.
– user1106925
Apr 20 '17 at 17:01
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I may be asking a basic question but I don't know how to clear such a confusion through google search
this below code works fine and count words, so I was trying to understand further
var words = chunk.trim().split(' ');
var counts = {};
// Count words
words.forEach(function (word) {
word = word.trim();
if (word.length) {
if (!counts[word]) {
counts[word] = 0;
}
counts[word]++;
}
});
then why not this below gives me 1,2 . I am expecting 1,2 and its giving me 2,2
var a = function(){
var a, b;
var obj = {}
obj[a]=1;
obj[b]=2;
console.log(obj[a]);
console.log(obj[b]);
}
var res = a();
javascript
I may be asking a basic question but I don't know how to clear such a confusion through google search
this below code works fine and count words, so I was trying to understand further
var words = chunk.trim().split(' ');
var counts = {};
// Count words
words.forEach(function (word) {
word = word.trim();
if (word.length) {
if (!counts[word]) {
counts[word] = 0;
}
counts[word]++;
}
});
then why not this below gives me 1,2 . I am expecting 1,2 and its giving me 2,2
var a = function(){
var a, b;
var obj = {}
obj[a]=1;
obj[b]=2;
console.log(obj[a]);
console.log(obj[b]);
}
var res = a();
javascript
javascript
edited Apr 21 '17 at 13:46
asked Apr 20 '17 at 16:55
user1207289
1,19321835
1,19321835
1
Bothaandbareundefined, so the only property you're setting onobjhas the name "undefined", so it gets the last value you set. All object properties are basically strings (except Symbols), so whatever you give for a property gets converted to a string.
– user1106925
Apr 20 '17 at 16:58
Various ways to test this would be toconsole.log()the values ofaandb, and to log the result ofObject.keys(obj)to see what keys you gave it.
– user1106925
Apr 20 '17 at 17:01
add a comment |
1
Bothaandbareundefined, so the only property you're setting onobjhas the name "undefined", so it gets the last value you set. All object properties are basically strings (except Symbols), so whatever you give for a property gets converted to a string.
– user1106925
Apr 20 '17 at 16:58
Various ways to test this would be toconsole.log()the values ofaandb, and to log the result ofObject.keys(obj)to see what keys you gave it.
– user1106925
Apr 20 '17 at 17:01
1
1
Both
a and b are undefined, so the only property you're setting on obj has the name "undefined", so it gets the last value you set. All object properties are basically strings (except Symbols), so whatever you give for a property gets converted to a string.– user1106925
Apr 20 '17 at 16:58
Both
a and b are undefined, so the only property you're setting on obj has the name "undefined", so it gets the last value you set. All object properties are basically strings (except Symbols), so whatever you give for a property gets converted to a string.– user1106925
Apr 20 '17 at 16:58
Various ways to test this would be to
console.log() the values of a and b, and to log the result of Object.keys(obj) to see what keys you gave it.– user1106925
Apr 20 '17 at 17:01
Various ways to test this would be to
console.log() the values of a and b, and to log the result of Object.keys(obj) to see what keys you gave it.– user1106925
Apr 20 '17 at 17:01
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
Why doesn't this give me
1,2? I am expecting1,2and it's giving me2,2
This is happening because a == b; they are both undefined because you never assigned them values on the line var a, b. When you omit the = assignment in var declarations, the default value of undefined is used instead, meaning that your line becomes var a = undefined, b = undefined.
Thus, the second property assignment (obj[b] = 2) is actually overwriting the first (obj[a] = 1). Your obj only contains a single key, and after running your code its overall structure looks like:
{
'undefined': 2
}
var a = function () {
var a, b
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
}
var res = a()If you want the results you expected, you need to use distinct keys, like the strings 'a' and 'b':
var a = function() {
var a = 'a', b = 'b'
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
console.log(obj[a], obj[b])
}
var res = a()
thanks , so why doescounts[word] = 0;work . I don't see any line in the whole code likevar word = 'word' ;in the word count example ( i posted partial code)
– user1207289
Apr 20 '17 at 17:19
1
TheArray.prototype.forEachfunction automatically passes awordvalue to the callback (here I refer to thefunction (word) { ... }), and it does so once for each element found in thewordsarray. Since thewordsarray can contain multiple distinct words, key collision does not always occur when doing assignments.
– gyre
Apr 20 '17 at 17:21
1
Why does counts[word] work? The reason is that due to notation, JS tries to add a property into counts object after checking that it didn't exist(if condition). So, counts[word] = 0 statement is doing three things, 1) evaluate the value of word variable(if not a string then convert to string), 2) add that evaluation, say it is "abc" as a property to counts object. So it becomes, counts.abc. 3) Set the value of the property to 0 so, counts.abc is equal to 0. Later, its count is incremented if found again.
– Pankaj Shukla
Apr 20 '17 at 18:35
add a comment |
up vote
6
down vote
Because you're only declaring (not assigning) both a and b, and in javascript a not assigned variable is implicitely undefined.
you're essentially referring to obj[undefined] twice
var a,b; //here both the variables are undefined
obj[a]=1;
obj[b]=2; //you're overriding the variable
console.log(obj[a]); //2
console.log(obj[b]); //2
add a comment |
up vote
1
down vote
When you say obj[a], javascript tries to create a property on obj with the value in a. Since value in a is undefined, the string representation is "undefined" and hence a property undefined is created on obj.
Since b also happens to have undefined value, you are essentially writing 2 onto the same property undefined.
Hence you get 2 both the times.
Actually if you log obj and see the value in console, you would see:
Object {undefined: 2}
undefined:2
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Why doesn't this give me
1,2? I am expecting1,2and it's giving me2,2
This is happening because a == b; they are both undefined because you never assigned them values on the line var a, b. When you omit the = assignment in var declarations, the default value of undefined is used instead, meaning that your line becomes var a = undefined, b = undefined.
Thus, the second property assignment (obj[b] = 2) is actually overwriting the first (obj[a] = 1). Your obj only contains a single key, and after running your code its overall structure looks like:
{
'undefined': 2
}
var a = function () {
var a, b
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
}
var res = a()If you want the results you expected, you need to use distinct keys, like the strings 'a' and 'b':
var a = function() {
var a = 'a', b = 'b'
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
console.log(obj[a], obj[b])
}
var res = a()
thanks , so why doescounts[word] = 0;work . I don't see any line in the whole code likevar word = 'word' ;in the word count example ( i posted partial code)
– user1207289
Apr 20 '17 at 17:19
1
TheArray.prototype.forEachfunction automatically passes awordvalue to the callback (here I refer to thefunction (word) { ... }), and it does so once for each element found in thewordsarray. Since thewordsarray can contain multiple distinct words, key collision does not always occur when doing assignments.
– gyre
Apr 20 '17 at 17:21
1
Why does counts[word] work? The reason is that due to notation, JS tries to add a property into counts object after checking that it didn't exist(if condition). So, counts[word] = 0 statement is doing three things, 1) evaluate the value of word variable(if not a string then convert to string), 2) add that evaluation, say it is "abc" as a property to counts object. So it becomes, counts.abc. 3) Set the value of the property to 0 so, counts.abc is equal to 0. Later, its count is incremented if found again.
– Pankaj Shukla
Apr 20 '17 at 18:35
add a comment |
up vote
1
down vote
accepted
Why doesn't this give me
1,2? I am expecting1,2and it's giving me2,2
This is happening because a == b; they are both undefined because you never assigned them values on the line var a, b. When you omit the = assignment in var declarations, the default value of undefined is used instead, meaning that your line becomes var a = undefined, b = undefined.
Thus, the second property assignment (obj[b] = 2) is actually overwriting the first (obj[a] = 1). Your obj only contains a single key, and after running your code its overall structure looks like:
{
'undefined': 2
}
var a = function () {
var a, b
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
}
var res = a()If you want the results you expected, you need to use distinct keys, like the strings 'a' and 'b':
var a = function() {
var a = 'a', b = 'b'
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
console.log(obj[a], obj[b])
}
var res = a()
thanks , so why doescounts[word] = 0;work . I don't see any line in the whole code likevar word = 'word' ;in the word count example ( i posted partial code)
– user1207289
Apr 20 '17 at 17:19
1
TheArray.prototype.forEachfunction automatically passes awordvalue to the callback (here I refer to thefunction (word) { ... }), and it does so once for each element found in thewordsarray. Since thewordsarray can contain multiple distinct words, key collision does not always occur when doing assignments.
– gyre
Apr 20 '17 at 17:21
1
Why does counts[word] work? The reason is that due to notation, JS tries to add a property into counts object after checking that it didn't exist(if condition). So, counts[word] = 0 statement is doing three things, 1) evaluate the value of word variable(if not a string then convert to string), 2) add that evaluation, say it is "abc" as a property to counts object. So it becomes, counts.abc. 3) Set the value of the property to 0 so, counts.abc is equal to 0. Later, its count is incremented if found again.
– Pankaj Shukla
Apr 20 '17 at 18:35
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Why doesn't this give me
1,2? I am expecting1,2and it's giving me2,2
This is happening because a == b; they are both undefined because you never assigned them values on the line var a, b. When you omit the = assignment in var declarations, the default value of undefined is used instead, meaning that your line becomes var a = undefined, b = undefined.
Thus, the second property assignment (obj[b] = 2) is actually overwriting the first (obj[a] = 1). Your obj only contains a single key, and after running your code its overall structure looks like:
{
'undefined': 2
}
var a = function () {
var a, b
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
}
var res = a()If you want the results you expected, you need to use distinct keys, like the strings 'a' and 'b':
var a = function() {
var a = 'a', b = 'b'
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
console.log(obj[a], obj[b])
}
var res = a()
Why doesn't this give me
1,2? I am expecting1,2and it's giving me2,2
This is happening because a == b; they are both undefined because you never assigned them values on the line var a, b. When you omit the = assignment in var declarations, the default value of undefined is used instead, meaning that your line becomes var a = undefined, b = undefined.
Thus, the second property assignment (obj[b] = 2) is actually overwriting the first (obj[a] = 1). Your obj only contains a single key, and after running your code its overall structure looks like:
{
'undefined': 2
}
var a = function () {
var a, b
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
}
var res = a()If you want the results you expected, you need to use distinct keys, like the strings 'a' and 'b':
var a = function() {
var a = 'a', b = 'b'
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
console.log(obj[a], obj[b])
}
var res = a()var a = function () {
var a, b
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
}
var res = a()var a = function () {
var a, b
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
}
var res = a()var a = function() {
var a = 'a', b = 'b'
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
console.log(obj[a], obj[b])
}
var res = a()var a = function() {
var a = 'a', b = 'b'
var obj = {}
obj[a] = 1
obj[b] = 2
console.log(obj)
console.log(obj[a], obj[b])
}
var res = a()edited Apr 20 '17 at 17:13
answered Apr 20 '17 at 17:00
gyre
11.1k11333
11.1k11333
thanks , so why doescounts[word] = 0;work . I don't see any line in the whole code likevar word = 'word' ;in the word count example ( i posted partial code)
– user1207289
Apr 20 '17 at 17:19
1
TheArray.prototype.forEachfunction automatically passes awordvalue to the callback (here I refer to thefunction (word) { ... }), and it does so once for each element found in thewordsarray. Since thewordsarray can contain multiple distinct words, key collision does not always occur when doing assignments.
– gyre
Apr 20 '17 at 17:21
1
Why does counts[word] work? The reason is that due to notation, JS tries to add a property into counts object after checking that it didn't exist(if condition). So, counts[word] = 0 statement is doing three things, 1) evaluate the value of word variable(if not a string then convert to string), 2) add that evaluation, say it is "abc" as a property to counts object. So it becomes, counts.abc. 3) Set the value of the property to 0 so, counts.abc is equal to 0. Later, its count is incremented if found again.
– Pankaj Shukla
Apr 20 '17 at 18:35
add a comment |
thanks , so why doescounts[word] = 0;work . I don't see any line in the whole code likevar word = 'word' ;in the word count example ( i posted partial code)
– user1207289
Apr 20 '17 at 17:19
1
TheArray.prototype.forEachfunction automatically passes awordvalue to the callback (here I refer to thefunction (word) { ... }), and it does so once for each element found in thewordsarray. Since thewordsarray can contain multiple distinct words, key collision does not always occur when doing assignments.
– gyre
Apr 20 '17 at 17:21
1
Why does counts[word] work? The reason is that due to notation, JS tries to add a property into counts object after checking that it didn't exist(if condition). So, counts[word] = 0 statement is doing three things, 1) evaluate the value of word variable(if not a string then convert to string), 2) add that evaluation, say it is "abc" as a property to counts object. So it becomes, counts.abc. 3) Set the value of the property to 0 so, counts.abc is equal to 0. Later, its count is incremented if found again.
– Pankaj Shukla
Apr 20 '17 at 18:35
thanks , so why does
counts[word] = 0; work . I don't see any line in the whole code like var word = 'word' ; in the word count example ( i posted partial code)– user1207289
Apr 20 '17 at 17:19
thanks , so why does
counts[word] = 0; work . I don't see any line in the whole code like var word = 'word' ; in the word count example ( i posted partial code)– user1207289
Apr 20 '17 at 17:19
1
1
The
Array.prototype.forEach function automatically passes a word value to the callback (here I refer to the function (word) { ... }), and it does so once for each element found in the words array. Since the words array can contain multiple distinct words, key collision does not always occur when doing assignments.– gyre
Apr 20 '17 at 17:21
The
Array.prototype.forEach function automatically passes a word value to the callback (here I refer to the function (word) { ... }), and it does so once for each element found in the words array. Since the words array can contain multiple distinct words, key collision does not always occur when doing assignments.– gyre
Apr 20 '17 at 17:21
1
1
Why does counts[word] work? The reason is that due to notation, JS tries to add a property into counts object after checking that it didn't exist(if condition). So, counts[word] = 0 statement is doing three things, 1) evaluate the value of word variable(if not a string then convert to string), 2) add that evaluation, say it is "abc" as a property to counts object. So it becomes, counts.abc. 3) Set the value of the property to 0 so, counts.abc is equal to 0. Later, its count is incremented if found again.
– Pankaj Shukla
Apr 20 '17 at 18:35
Why does counts[word] work? The reason is that due to notation, JS tries to add a property into counts object after checking that it didn't exist(if condition). So, counts[word] = 0 statement is doing three things, 1) evaluate the value of word variable(if not a string then convert to string), 2) add that evaluation, say it is "abc" as a property to counts object. So it becomes, counts.abc. 3) Set the value of the property to 0 so, counts.abc is equal to 0. Later, its count is incremented if found again.
– Pankaj Shukla
Apr 20 '17 at 18:35
add a comment |
up vote
6
down vote
Because you're only declaring (not assigning) both a and b, and in javascript a not assigned variable is implicitely undefined.
you're essentially referring to obj[undefined] twice
var a,b; //here both the variables are undefined
obj[a]=1;
obj[b]=2; //you're overriding the variable
console.log(obj[a]); //2
console.log(obj[b]); //2
add a comment |
up vote
6
down vote
Because you're only declaring (not assigning) both a and b, and in javascript a not assigned variable is implicitely undefined.
you're essentially referring to obj[undefined] twice
var a,b; //here both the variables are undefined
obj[a]=1;
obj[b]=2; //you're overriding the variable
console.log(obj[a]); //2
console.log(obj[b]); //2
add a comment |
up vote
6
down vote
up vote
6
down vote
Because you're only declaring (not assigning) both a and b, and in javascript a not assigned variable is implicitely undefined.
you're essentially referring to obj[undefined] twice
var a,b; //here both the variables are undefined
obj[a]=1;
obj[b]=2; //you're overriding the variable
console.log(obj[a]); //2
console.log(obj[b]); //2
Because you're only declaring (not assigning) both a and b, and in javascript a not assigned variable is implicitely undefined.
you're essentially referring to obj[undefined] twice
var a,b; //here both the variables are undefined
obj[a]=1;
obj[b]=2; //you're overriding the variable
console.log(obj[a]); //2
console.log(obj[b]); //2
edited Nov 10 at 22:31
answered Apr 20 '17 at 16:58
Karim
4,3691719
4,3691719
add a comment |
add a comment |
up vote
1
down vote
When you say obj[a], javascript tries to create a property on obj with the value in a. Since value in a is undefined, the string representation is "undefined" and hence a property undefined is created on obj.
Since b also happens to have undefined value, you are essentially writing 2 onto the same property undefined.
Hence you get 2 both the times.
Actually if you log obj and see the value in console, you would see:
Object {undefined: 2}
undefined:2
add a comment |
up vote
1
down vote
When you say obj[a], javascript tries to create a property on obj with the value in a. Since value in a is undefined, the string representation is "undefined" and hence a property undefined is created on obj.
Since b also happens to have undefined value, you are essentially writing 2 onto the same property undefined.
Hence you get 2 both the times.
Actually if you log obj and see the value in console, you would see:
Object {undefined: 2}
undefined:2
add a comment |
up vote
1
down vote
up vote
1
down vote
When you say obj[a], javascript tries to create a property on obj with the value in a. Since value in a is undefined, the string representation is "undefined" and hence a property undefined is created on obj.
Since b also happens to have undefined value, you are essentially writing 2 onto the same property undefined.
Hence you get 2 both the times.
Actually if you log obj and see the value in console, you would see:
Object {undefined: 2}
undefined:2
When you say obj[a], javascript tries to create a property on obj with the value in a. Since value in a is undefined, the string representation is "undefined" and hence a property undefined is created on obj.
Since b also happens to have undefined value, you are essentially writing 2 onto the same property undefined.
Hence you get 2 both the times.
Actually if you log obj and see the value in console, you would see:
Object {undefined: 2}
undefined:2
answered Apr 20 '17 at 17:08
Pankaj Shukla
1,7422414
1,7422414
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43525555%2fwhy-do-these-two-dynamic-property-assignments-collapse-into-a-single-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Both
aandbareundefined, so the only property you're setting onobjhas the name "undefined", so it gets the last value you set. All object properties are basically strings (except Symbols), so whatever you give for a property gets converted to a string.– user1106925
Apr 20 '17 at 16:58
Various ways to test this would be to
console.log()the values ofaandb, and to log the result ofObject.keys(obj)to see what keys you gave it.– user1106925
Apr 20 '17 at 17:01