0

I was taught to use "let" to initialize arrays in JavaScript, but I've recently discovered that "var" can be used as well.

var evenNumbers=[2,4,6,8];

I know that it's possible to initialize a function in JavaScript using var, as in

var hello=function(){};

so naturally, I assumed that the "var" being used to initialize the variable refers to the name of the array, in this case evenNumbers.

However I also recently learned that to initialize arrays in C, which I think of as the grandfather of Java-type languages, the type of variable used in the array is used to initialize the array call.

int evenNumbers[]={2,4,6,8};

Obviously in this case, int refers to the elements of the list, since an array is not an int.

I therefore assumed that var before an array call in JavaScript refers to the elements of the list. I tried to test it by applying the wrong strong type to a new JavaScript variable, like

int newYearsResolutions=["Stop procrastinating"];

Which gives me an unexpected identifier, but that's not too helpful since an array is not an int nor is "Stop procrastinating" an int. I then tried

int evenNumbers=[2,4];

and this gives me the same error, leading me back to my original conclusion that the var being named here is "evenNumbers" and not the ints 2 and 4, but I still feel like I might be missing something.

So, var evenNumbers=[2,4] appears to name the evenNumbers variable and not the elements of the array. I just want to double-check that that's the case.

7
  • 1
    What makes you think that Javascript is a "Java-like language"? It isn't, the Java bit is simply from an old marketing stunt netscape did in the 90s Commented Feb 8, 2019 at 13:42
  • Here's a list of Statements and declarations from MDN. There is no int keyword. Commented Feb 8, 2019 at 13:44
  • JavaScript, until recently, only had var declarations. Let and const are new with ES6. But legacy will allow for var to continue as valid code Commented Feb 8, 2019 at 13:44
  • Don't worry much about these details. You can use var instead of let or const without any trouble. It's simpler than that. var is variable and it can store any type of data. You don't have to specify the type. It could be: var varName = [1, 2, 3]. (array), var varName = "text". (string). And so on: object, function, int, boolean. Commented Feb 8, 2019 at 13:45
  • 1
    And yeah, what Liam said ;) JavaScript is not Java. Age old wisdom: Java is to JavaScript as ham is to hamster. Commented Feb 8, 2019 at 13:45

1 Answer 1

2

JavaScript is not a typed language. int isn't a reserved word, and thus is not doing anything. var, let, const are all ways to assign variables - regardless of type.

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

2 Comments

What I mean to ask is, is the array the var, or is it a list of vars? Or both?
the var is a reference to the array. Just think of it like C. It's just a pointer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.