Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
186
rated 0 times [  193] [ 7]  / answers: 1 / hits: 77511  / 12 Years ago, mon, january 7, 2013, 12:00:00

The title says it all, but I will provide more clarification:



After seeing many samples of javascript where all variables are declared as type var, and seeing support for other datatypes, why aren't variables of a specific datatype declared as such? Meaning, why isn't this:



string hello = 'Hello, World'



used instead of



var hello = 'Hello, World'



Looking at sites like OReilly Javascript shows that there are reserved words for other types. Again, why aren't they used? Wouldn't it make lines like this: typeof(variable)==='string'; no longer needed?


More From » variables

 Answers
28

Quite simply, JavaScript variables do not have types. The values have types.



The language permits us to write code like this:



var foo = 42;
foo = 'the answer';
foo = function () {};


So it would be pointless to specify the type in a variable declaration, because the type is dictated by the variable's value. This fairly common in dynamic languages.


[#81027] Sunday, January 6, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianod

Total Points: 667
Total Questions: 106
Total Answers: 92

Location: Jordan
Member since Thu, Aug 5, 2021
3 Years ago
lucianod questions
;