Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use typed variables in javascript?

Tags:

javascript

Is there any way to use typed variables in javascript? Like integer, string, float...

like image 340
errgod Avatar asked Mar 11 '12 22:03

errgod


People also ask

How are variables typed in JavaScript?

JavaScript is a loosely-typed language, so a variable can store any type value. Variable names are case-sensitive. Variable names can contain letters, digits, or the symbols $ and _. It cannot start with a digit 0 - 9.

Can I use type in JavaScript?

TypeScript fans can still make use of JavaScript tutorials, since JavaScript can fit very well into their TypeScript files, while the opposite is not true.

How do you call a variable in JavaScript?

After declaring a variable or function with the var keyword, you can call it at any time by invoking its name.

Do variables have types in JavaScript?

tl;dr: In JavaScript, variables don't have types, but values do. The above code is perfectly valid because in JavaScript, variables don't have types. Variables can hold arbitrary values, and these values have types. Think of variables as labeled boxes whose contents can change over time.


2 Answers

JavaScript variables are not typed.

JavaScript values are, though. The same variable can change (be assigned a new value), for example, from uninitialized to number to boolean to string (not that you'd want to do this!):

var x;       // undefined
x = 0;       // number
x = true;    // boolean
x = "hello"; // string
like image 85
John Pick Avatar answered Oct 20 '22 11:10

John Pick


Javascript is dynamically typed, whereas other languages, C# and Java for example, are statically typed. This means that in Javascript variables can be reassigned to values of any type, and thus that you don't ever need to explicitly denote the type of variables or the return type of functions. If you saw code like this in a statically typed language

int x = 5;
x = "hello";

you would rightfully expect the compiler to start throwing up a big nasty TypeError. Javascript, on the other hand, will happily run this code even though the type changed.

var x = 5;
x = "hello";

Because variables can change types, the compiler can't know as much information about them. You should expect the tools for Javascript to be inferior to those of Java/C# as far as useful niceties like code completion go. Fewer mistakes will be caught at compile time and you will have to do more runtime debugging than you are probably used to.

That said, this also allows you to be more free with your variables and you can change types at will, which can often be convenient. You can write code like this if you want:

var x;               //typeof x === "undefined"
x = "Hello, world!"; //typeof x === "string"
x = 42;              //typeof x === "number"
x = false;           //typeof x === "boolean"
x = {};              //typeof x === "object"
like image 42
Peter Olson Avatar answered Oct 20 '22 13:10

Peter Olson