A very strange behavior of const keyword.

35 views
Skip to first unread message

Dongsung Kim

unread,
Aug 13, 2015, 4:26:58 AM8/13/15
to v8-users
I was working on something with node.js, noticed something strange with const keyword. Please refer to the test case script attached with this message.

The logic is straightforward. Make an array of integers from 1 to 100, slice the array every 10 numbers; 1~10 / 11~20 / ..., and print the first number of each subarray; 1, 11, 21, 31, 41, and so on.

The test cases are basically identical to each other with the exception of the sliced subarray is stored. In case 0, you see the subarray is stored as a variable, and the result comes out as expected.

const.js:13
for(var i=0; i<length; i+=step) {
 
var c = array.slice(i, i+step);
 res
.push(c[0]);
}
console
.log('var ' + res.join(' '));

Result
var               1 11 21 31 41 51 61 71 81 91

However as we move on, strange things start to happen. In case 1, now we store each as a constant, which is block-scoped, and the result is not something you'd expect.

const.js:25
for(var i=0; i<length; i+=step) {
 
const c = array.slice(i, i+step);
 res
.push(c[0]);
}
console
.log('const ' + res.join(' '));

Result
const             1 1 1 1 1 1 1 1 1 1

Case 2 and 3 are trying to mitigate this behavior. Wrapping the const code line with a function, or even declaring another block-scoped type like let or const before the line does the job.

const.js:37
for(var i=0; i<length; i+=step) {
 (function() {
   
const c = array.slice(i, i+step);
   res
.push(c[0]);
 })();
}
console
.log('const in function ' + res.join(' '));

const.js:53
for(var i=0; i<length; i+=step) {
 let v;
 
const c = array.slice(i, i+step);
 res
.push(c[0]);
}
console
.log('let and const ' + res.join(' '));

Result 
const in function 1 11 21 31 41 51 61 71 81 91 
let and const     1 11 21 31 41 51 61 71 81 91

The behavior was tested on OS X with Google Chrome 44.0.2403.155, Canary 46.0.2481.0, node.js 0.12.7 with harmony flag, io.js 3.0.0. As Apple Safari and Mozilla Firefox work as expected, I believe it's a bug in V8's end.

Don

const.js

Andreas Rossberg

unread,
Aug 13, 2015, 4:52:03 AM8/13/15
to v8-u...@googlegroups.com
You currently need to be in strict mode to get ES6 `const` semantics in V8/Chrome. In sloppy mode, you still get the crazy legacy web semantics that isn't block-scoped (similarly with function decls). We have shied away from changing that so far, because we fear it might break the web (on mobile in particular). We have added use counters to Chrome 44+ to get some data about potential breakage.

/Andreas


--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dongsung Kim

unread,
Aug 13, 2015, 5:05:55 AM8/13/15
to v8-users
Andreas,

Oh, that's why! So glad it's a known issue you guys are working on. I hope for a fix since it's pretty confusing; the error popped up with let keyword said things of const keyword, but nothing from const. Would it be terrible to put up a warning when const keywords is spotted in non-strict mode?

Thanks,
Don

Andreas Rossberg

unread,
Aug 13, 2015, 5:42:39 AM8/13/15
to v8-u...@googlegroups.com
On 13 August 2015 at 11:05, Dongsung Kim <kid...@gmail.com> wrote:
Oh, that's why! So glad it's a known issue you guys are working on. I hope for a fix since it's pretty confusing; the error popped up with let keyword said things of const keyword, but nothing from const. Would it be terrible to put up a warning when const keywords is spotted in non-strict mode?

That would certainly be useful. But V8 currently has no notion of warning, and we didn't feel like introducing one just for this temporary problem. In general, using strict mode is highly recommended anyway. ;)

/Andreas


On Thursday, August 13, 2015 at 5:52:03 PM UTC+9, Andreas Rossberg wrote:
You currently need to be in strict mode to get ES6 `const` semantics in V8/Chrome. In sloppy mode, you still get the crazy legacy web semantics that isn't block-scoped (similarly with function decls). We have shied away from changing that so far, because we fear it might break the web (on mobile in particular). We have added use counters to Chrome 44+ to get some data about potential breakage.

/Andreas

--

Dongsung Kim

unread,
Aug 13, 2015, 5:56:10 AM8/13/15
to v8-users
I understand. Hope the potential breakage turns out to be minimum so the confusion can be resolved. In the mean time people would look up this mailing list as a reference I guess.

Thank you for the work. I appreciate every single bit!

Don
Reply all
Reply to author
Forward
0 new messages