user310291
user310291

Reputation: 38190

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

Upvotes: 1

Views: 361

Answers (4)

user13028341
user13028341

Reputation:

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

Upvotes: 0

Sebastian Kaczmarek
Sebastian Kaczmarek

Reputation: 8515

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.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074595

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];

Upvotes: 2

Peter
Peter

Reputation: 68

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.

Upvotes: 0

Related Questions