Reputation: 21
I was doing JavaScript, and I was wondering : can we declare a type for our variable ? if yes, how?
Upvotes: 2
Views: 6173
Reputation: 910
see http://www.w3schools.com/js/js_datatypes.asp
types in javascript are automatically and when you assign something to a variable for example
var myVar = "Hello World";
the type of myVar changed automatically to string and you can see it with the typeof function
Upvotes: 0
Reputation: 6554
Technically you don't need to/can't because JavaScript is a loosely written coding language. Though when you declare a variable it is that datatype, only difference is that we can overwrite these values and the datatype is updated.
var one = 15; //int
var two = "Hello";//string
var three = false; //boolean
var four = ["hello","world"];//object Array
var five = {prop:"key"}; //object Object
Now we can also rewrite these and the datatype will be changed. To check for the data type do
console.log(typeof one);
Upvotes: 0
Reputation:
You can not defive the type of a Javascript variable Because it can contain everything from text to number
Upvotes: 0