Solved

What is var

Posted on 2012-03-31
6
257 Views
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?
0
Comment
Question by:tonelm54
6 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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/
0
 
LVL 82

Expert Comment

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

Expert Comment

by:Proculopsis
Comment Utility
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

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 25

Expert Comment

by:Lee Savidge
Comment Utility
0
 
LVL 12

Expert Comment

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

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

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

Expert Comment

by:Lee Savidge
Comment Utility
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.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now