|In this chapter you will learn :
- What are variables?
- Scope of variables.
- Naming variables
- Various operators and expressions.
Working with Operators and Variables
- Variable names can begin with an uppercase letter (A through Z), lowercase letter (a through z), and underscore character( _ ) or dollar sign character( $ ).
- The remaining characters can consist of letters, the underscore character, the dollar sign character or digits 0 to 9.
- Variable names are case sensitive.
It consists of integer and floating point numbers and the special NaN (Not a Number) value.
But hexadecimal and octal integers are converted to decimal before they are displayed.
Floating-point literals require the use of either a decimal point or an integer followed by an exponent.
For example: 39, 12.25, -220, -34.54, 23E4
- It consists of logical values true and false.
- Logical operators can be used in Boolean expressions.
- It consists of string values that are enclosed in single or double quotes.
- A string is a sequence of zero or more characters.
- If a string begins with a single quote, it must end with a single quote.
- If a string begins with a double quote it must end with a double quote.
- It consists of a single value, i.e., null.
- Null identifies an empty or nonexistent reference.
- The use of null value prevents the errors from using uninitialized variables.
Variables can be declared using 'var' command. Although it is possible to declare variables by simply using them, declaring them ensures that programs are well organized and helps keep track of the scope of the variables. You can also simultaneously declare as well as assign value to the variable. The syntax for declaring variable is :
|var < variable name > = value ;
| var name;
var address = "house no. 88";
var pincode = 110044;s
- An array is an indexed list of elements or group of related variables.
- The length of an array is the number of elements that an array contains.
- The individual elements of an array are accessed by using the name of the array followed by the index value.
- The array element index starts with 0.
- The last array element index number is one less than the length of the array.
An array must be declared before it is used.
- Array can be declared as :
arrayName = new Array ( Array Length) or
arrayName = new Array( )
| Students = new Array ( 35 ) ;
Stud_Name = "James"