What is var

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
"You declare JavaScript variables with the var keyword:"  http://www.w3schools.com/js/js_variables.asp
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

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Lee SavidgeCommented:
Anuradha GoliSystems Development / Support SpecialistCommented:
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
Lee SavidgeCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.