简体   繁体   中英

How to reset chrome console variables

let says I have

const array = [1, 2, 3, 4];

I want to restart with

const array = [1, 2, 3, 4, 5];

So how to avoid (without closing and reopening console)

VM347:1 Uncaught SyntaxError: Identifier 'array' has already been declared at :1:1

I don't think you can, the console is fairly special but it is, fundamentally, an open-ended execution context. You can't redeclare a const within the same execution context unless it's in a nested block. (And if you open a nested block in the console, you don't see the content evaluated until you close the block, so that wouldn't help.)

Instead, use let and leave off the let the second time:

let array = [1, 2, 3, 4];
// ...
array = [1, 2, 3, 4, 5];

Or if that's a big problem, use var since you're allowed to repeat it.

var array = [1, 2, 3, 4];
// ...
var array = [1, 2, 3, 4, 5];

Change const to var .

Using const means that the values cannot be changed after being initialized.

var array = [1, 2, 3, 4];

So when you want to change the values do:

array = [1, 2, 3, 4, 5];

So now it should work.

You can't. It's the same as with the Node.js terminal. If it's declared, you need to reset the context by refreshing the console.

const declares a read-only named constant, you should be using the let statement in this case as follow:

let array = [1, 2, 3, 4]

// Reassign the value of 'array'
array = [1, 2, 3, 4, 5]

// Log the result
console.log(array)

Result:

1, 2, 3, 4, 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM