| Overview |
| |
In this chapter you will learn :
- What are variables?
- Various datatypes supported by JavaScript.
- Scope of variables.
- Naming variables
- Various operators and expressions.
|
|
| |
Working with Operators and Variables
JavaScript offers the same programming capabilities found in most programming languages like creating variables, constants, programming constructs, user defined functions and so on. Variables are used to store values that can be used in other parts of a program. Variables always have a name associated with them through which they can be referenced later. So, a variable is a named memory location that is used to hold data which can be modified by the program.
- 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.
- JavaScript does not allow the data type of the variable to be declared when a variable is created.
JavaScript supports four primitive types of values and supports complex types such as arrays and objects. Variables supported by JavaScript consists of :
Number
It consists of integer and floating point numbers and the special NaN (Not a Number) value.
Integer variables can also be represented in JavaScript in decimal, hexadecimal, and octal form.
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
Boolean
- It consists of logical values true and false.
- Logical operators can be used in Boolean expressions.
- JavaScript automatically converts the Boolean values true and false into 1 and 0, respectively, when used with numerical expressions.
String
- 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.
For example : "JavaScript", 'House number-67'
Null
- 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.
Creating 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 :
| Syntax |
| var < variable name > = value ; |
| |
| Example |
var name;
var address = "house no. 88";
var pincode = 110044;s |
Arrays
- Arrays are JavaScript objects that are capable of storing a sequence of values.
- 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( )
JavaScript automatically extends the length of an array when new array elements are initialized.
| Example |
Students = new Array ( 35 ) ;
Stud_Name[2] = "James" |
| |