2

I was doing JavaScript, and I was wondering : can we declare a type for our variable ? if yes, how?

1
  • Using JSDoc with Google Closure Compiler counts in? Commented Oct 9, 2014 at 19:59

4 Answers 4

3

No javascript variables are not typed.

Sign up to request clarification or add additional context in comments.

1 Comment

This is best for understanding, but not technically correct. It is possible to change the type... or even use this when you declare the variable... but you really shouldn't need to... w3schools.com/js/js_type_conversion.asp for more information.
0

You can not defive the type of a Javascript variable Because it can contain everything from text to number

Comments

0

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);

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.