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.

reflektor
  • 21
  • 8
  • 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 – Liam Feb 08 '19 at 13:42
  • Here's a list of [Statements and declarations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements) from MDN. There is no `int` keyword. – adiga Feb 08 '19 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 – Doug Feb 08 '19 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. – Azametzin Feb 08 '19 at 13:45
  • [....it was renamed JavaScript when it was deployed in the Netscape Navigator 2.0 beta 3 in December. The final choice of name caused confusion, giving the impression that the language was a spin-off of the Java programming language, and the choice has been characterized as a marketing ploy by Netscape to give JavaScript the cachet of what was then the hot new Web programming language.](https://en.wikipedia.org/wiki/JavaScript#Beginnings_at_Netscape) – Liam Feb 08 '19 at 13:45
  • are you confusing JavaScript with Java? `int` is not a way to declare a variable in JS. `var` and `let` are similar except they dont have the same scope visibility (and `let` is more recent). They just declare the identifier, what you put after the `=` is any type you want. Actually in JS var declarations are "hoisted" so that the declaration and the assignment don't happen at the same moment – Kaddath Feb 08 '19 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. – Doug Feb 08 '19 at 13:45

1 Answers1

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.

ehutchllew
  • 940
  • 8
  • 14