Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Why var keyword is used in javascript rather than using string or number
Posted
Comments
PIEBALDconsult 16-Feb-15 9:27am    
Laziness probably, same as in C#.
Sergey Alexandrovich Kryukov 16-Feb-15 11:14am    
Sorry, not even close. In C#, this is not "laziness", this is type inference. Yes, kinda related to "laziness"...

But in JavaScript, there are no type declarations (and even types in the same sense of this word). Please see my answer. This is a very non-trivial thing.

—SA
PIEBALDconsult 16-Feb-15 11:18am    
Sounds like laziness to me, a poorly implemented language. Even other "scripting" languages (Perl, DCL, etc.) don't require a useless keyword that does nothing.
Sergey Alexandrovich Kryukov 16-Feb-15 11:40am    
My point is not about "laziness". I say that C# "var" and JavaScript "var" have nothing in common. And JavaScript is not poorly designed at all, especially compared to languages you listed. "var" does a lot; please see my answer. (Of course this would not be an argument against your criticism of JavaScript, but about understanding. You need to gain some understanding before criticizing anyway, right?)
—SA
Maciej Los 16-Feb-15 9:33am    
For the same reason as Dim is used in VB...

Var means variable. Why it is used in JavaScript is partially explained here: http://www.w3schools.com/js/js_variables.asp[^] (not recommended as Sergey mentioned in the comment to the answer)

For further information, please see:
http://www.tutorialspoint.com/javascript/javascript_variables.htm[^]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types[^]

[EDIT]
As always, Wikipedia rush to rescue of drowning ;)
http://en.wikipedia.org/wiki/JavaScript_syntax#Variables[^]

Quote:

Variables in standard JavaScript have no type attached, and any value can be stored in any variable. Variables are declared with a var statement, multiple variables can be declared at once.


So, one keyword is used to declare many data types ;)
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 16-Feb-15 10:53am    
Hi Maciej,

Please listed for a good advice: at least remove the first reference fro your answer. You need to know that w3school.com is notorious with inaccurate information. I do use this site often for convenience, but this particular explanation is what is called, in CodeProject terms, "inaccurate/misleading".

Good explanation can be found here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var.

"var" is quite non-trivial. Note that in JavaScript strict mode "use strict"; "var" is done a must, and later version of JavaScript introduced a lexical-scope "let" and "const".

—SA
Sergey Alexandrovich Kryukov 16-Feb-15 11:11am    
Please read my Solution 2. Perhaps even you, Maciej, will be surprised. I personally was surprised when I first faced such things related to var.
—SA
Wendelius 19-Feb-15 12:07pm    
Nice answer
Maciej Los 19-Feb-15 13:52pm    
Thank you, Mika ;)
Consider the following:
JavaScript
var x = "outer scope";

(function() {
     alert(x);
     var x = "inner scope";
})()

Do you think that alert will output "outer scope"? No! It will happen only if you uncomment inner var x. But how the code written after the statement can affect it? Yes, it can. JavaScript is a very non-trivial thing; it is said to be the most misunderstood language in the world. The second var declaration tells the interpreter to use inner scope, which makes x on previous line undefined. So, JavaScript is not the interpreter which merely interprets and executes everything line by line.

The var keyword is used to resolve scope. Moreover, in JavaScript strict mode it is considered, var is made a must. And new let keyword introduced in in v.1.7 acts differently; if you used let instead of var in the function I show, it would generate an error. Unlike var, let is sensitive to the lexical scope.

All these features are the part of effort to introduce better support of JavaScript programming activity, to make it more reliable.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let[^].

—SA
 
Share this answer
 
v3
Comments
Maciej Los 16-Feb-15 12:15pm    
I think, we both misunderstood OP's question. If i understand it now, OP is asking about why var is used to declare variable which can hold string, integer, etc. One keyword to declare many types of data (variables). So, the question should be: why JavaScript does not need to declare variable corresponding to its type.
Sergey Alexandrovich Kryukov 16-Feb-15 12:50pm    
Let's see what if the inquirer clarifies on that. Indeed, the question look weird, as if the inquirer was confused about much more elementary issues, not related to "var" but to using a variable itself (with a variable name).
—SA
Maciej Los 16-Feb-15 13:52pm    
I revisited your answer, Sergey. Decided to up-vote it, because of explanation about scope of variables and JavaScript interpreter. +5!
Sergey Alexandrovich Kryukov 16-Feb-15 13:54pm    
Thank you, Maciej. I just though it would be good to share regardless of the inquirer's question, interesting and important stuff.
—SA
Vivek S Kale 5-Mar-15 4:23am    
Hi
maciej
you have perfectly understood my question
i.e.why JavaScript (does not need to/not) declare variable corresponding to its type.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900