[Last Call] Learn how to a build a cloud-first strategyRegister Now


What is var

Posted on 2012-03-31
Medium Priority
Last Modified: 2012-04-02
Im trying to debug a few javascripts, and wondering if there is a way of finding out if a var is a string, int, long, float etc?

For example, if I use:-
var x=5;
var carname="Volvo";

Open in new window

Is it possible to detect what x is stored as, or carname?
Question by:tonelm54
LVL 83

Accepted Solution

leakim971 earned 2000 total points
ID: 37791038
vars are not typed (you can change their type at any moment),  you may use typeof keyword to know the current type of a var : https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof

var x=5;
alert( "type of x is " + typeof x);
var carname="Volvo";
alert( "type of x is " + typeof carname);

Open in new window

test page : http://jsfiddle.net/2VLW4/
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37792518
"You declare JavaScript variables with the var keyword:"  http://www.w3schools.com/js/js_variables.asp
LVL 20

Expert Comment

ID: 37792572
Noooooo, var is used to define a local variable - see below:

hello = "Hello ";
world = "World!"

function sample() {
    hello = "Goodnight ";
    var world = "Vienna!";

new sample();
alert( hello + world ); // => Goodnight World!

Open in new window

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 25

Expert Comment

by:Lee Savidge
ID: 37794953
LVL 12

Expert Comment

by:Anuradha Goli
ID: 37795583
var simple refers to variable means it can be changed during the code.
You can get data type of variable using typeof.

  <script type="text/javascript">
        var firstname;
        document.write("<br />");

returns number,
If firstname="12" then it returns string
LVL 25

Expert Comment

by:Lee Savidge
ID: 37795622
The question asker isn't asking what "var" is. The query is with respect to finding out the type which you can use typeof to determine that.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question