0

I am using node version 12.22.6.

I don't understand, how this code is wrong.
I'm probably missing some important basic thing, but just cannot figure it out.

const change_vars = (var) => {
    console.log(var + " is a " + typeof(var) + "\n");
}

const variables = [
    42,
    "42",
    {number: 42},
    {},
    true,
    undefined
]

variables.forEach(var => change_vars(var));

node vars.js

$ SyntaxError: Unexpected token 'var'

1
  • 3
    "var" is a reserved keyword in JS - you can't use it for a variable name (and function parameters are just a special type of variable) Commented Oct 14, 2021 at 20:59

4 Answers 4

1

You can't use a reserved word to name your variables, you can find the full list here:

But some examples include:

break
case
catch
class
const
continue
debugger
default
delete
do
else
export
extends
finally
for
function
if
import
in
instanceof
new
return
super
switch
this
throw
try
typeof
var
void
while
with
yield

Long story short makes sure your variables are not named the same as the reserved words.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much!!
1

var is a reserved keyword in javascript. Use another variable name.

1 Comment

Thank you! I'll use another name!
0

You can't name your variable var as that is a reserved word in javascript.

(Unrelated: I recommend using camel-case for variables; changeVars instead of change_vars)

1 Comment

Thank you! I'll change the variable and the function name!
0

var is a reserved keyword in JavaScript, you will have to rename your variable to something else: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#keywords

1 Comment

Thank you so much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.