Variables & Constants



index
Disabled back button Next Section
printable version

Section 1: Definition
Section 2: Declaring Variables
Section 3: Global and Local Variables
Section 4: Initialize all Variables

It is better to initialize your variables, because

var variableName = "Hello";
var var1 = 14, var2 = 12.50;

Section 5: Constants

Constants are named memory locations whose values cannot change during program execution.

Constants are often used to make a program more readable.

Constants are declared using the keyword const. The constant must be initialized with a constant expression when it is declared and cannot be modified thereafter.

const pi = 3.14159;

Section 6: Resources

Case Sensitivity

Definition: case sensitivity defines whether uppercase and lowercase letters are treated as distinct (case-sensitive) or equivalent (case-insensitive).

X

Undefined Type

Definition: A variable that has not been assigned a value has the value undefined. The data type of a variable that holds an undefined value is also 'undefined'. The undefined type is one of JavaScript's primitive types that has one value – undefined.

Undefined Type

Definition: A variable that has not been assigned a value has the value undefined. The data type of a variable that holds an undefined value is also 'undefined'. The undefined type is one of JavaScript's primitive types that has one value – undefined.

Semicolon Usage

JavaScript performs automatic semicolon insertion (ECMA Script specification section 7.9). That makes including semicolons in most of your code optional. However it is recommended that semicolons be used to end each statement.

If you examine the ECMA specification you will see that there are a series of rules for automatic semicolon insertion and that a script without semicolons will require considerable extra effort on the part of the parser working out where the semicolons should go. Explicitly putting a semicolon in the correct location will save the parser from having to work out where it should put them.

Therefore, including semicolons may help speed up the work of the compiler/interpreter so that the page can be rendered faster. Besides, it is always a good coding practice. Semicolons are also valuable for code readability and maintainability.

Finally, coding rigor tends to produce significantly better software. Along with proper commenting and clean presentation, the code becomes more readable and more maintainable, reducing cost over its product life.