Defining Variables in Javascript.
Javascript variables
How to define Javascript variables?
- JavaScript is a loosely typed language, meaning that no variable can clearly be defined with a specific datatype.
-
We are using the general type, var, for all datatype
(numbers, booleans, strings, objects and so on).
var myInteger=10; var myString="10"; var myboolean=true; // It is legal to omit the "var" keyword, but not recommended: myvariable=10;
-
The semicolon serves to separate statements from each other.
var counter = 3; var weight = 40; // or the same without var keyword: counter = 3; weight = 40;
- However you can omit the semicolon for each of your statements placed on a separate lines.
counter = 3 weight = 40
- BUT - It is not a god practice to omit the semicolon.
// For instance: return true; // is NOT the same as: return true;
-
You can also declare several variables and different
types of variables with the same var keyword:
<script type="text/javascript"> var a=4; var b="45", c=0, d=true; document.write("a: "+ a +" b: "+b); document.write(" c: "+ c +" d: "+d); </script>
The result should be:a: 4 b: 45 c: 0 d: true
Variable name and other identifiers in javascript.
-
An identifier is used to name variables and functions, and to provide labels for certain loops.
- An identifier must start with a letter, an underscore (_), or a dollar sign ($).
- Subsequent characters for the identifier can be a letter, a digit, an underscore (_), or a dollar sign ($).
// These names are all legal identifiers: i variable_name version13 _dummyName $myString
-
You cannot use reserved keywords as identifiers:
keyword keyword keyword keyword keyword break do if switch typeof case else in this var catch false instanceof throw void continue finally new true while default for null try with delete function return -
You should also avoid these words as identifiers:
keyword keyword keyword keyword keyword arguments encodeURI Infinity Object String Array Error isFinite parseFloat SyntaxError Boolean escape isNaN parseInt TypeError Date eval Math RangeError undefined decodeURI EvalError NaN ReferenceError unescape decodeURIComponent Function Number RegExp URIError
© 2010 by Finnesand Data.
All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the
correctness in the pages or source codes.
The risk of using this web-site pages or any program
codes from this website is entirely at the individual user.