Variables in ECMAScript are declared using the var keyword. Variable names must begin with a letter or an underscore followed by zero or more letters, numbers, and underscores. ECMAScript variables are case-sensitive.
ECMAScript supports weak typing. The programmer does not need to specify the type of a variable when declaring it; the interpreter automatically associates one of the five built-in ECMAScript types with the new variable based on the nature of the value assigned to it. These built-in types are shown in Table A-2.
Type | Description | Examples |
---|---|---|
number | either a floating-point or integer |
var e = 2.71828; var i = -1; |
Boolean | only true or false |
var b = true; |
string | a string of characters |
var s = "hello!"; |
function | a reference to ECMAScript code |
var f = write; |
object | a structured container of variables and/or functions |
var o = new Employee("Smith", "John", 35, writeEmployeeRecord); |
Depending on where a variable is declared, it can be a global or local variable. A global variable is visible from within all functions. A local variable is visible only within the function in which it is declared.
3.139.235.119