Search within TutorialsPark

Javascript Variables


Javascript variables are data that might change during the course of the program.


Javascript Variables

1. Variables are fundamental building blocks in a programming language, its a data item that represents a memory location on the computer.

2. Variables are similar to containers that can contain hold data items like numbers and string. Variables have a name, value and type

3. Values assigned to variables can change throughout the course of the program, while the values of literals remain constant.

3. Three types of data can be assigned to the variables.

numeric

string

Boolean

Example: Javascript - Variables

Note: Javascript comments can be used to Disable Scripts from being executed


Javascript Variables : Valid Names

1. There exists some rules for assigning valid names for variables.

2. The first character of a variable must be letter or an underscore(_) or dollar($)

3. Variable names are case-sensitive i.e Class, class, CLaSs are all different variable names.

Note: Javascript variables must be descriptive, inorder to make the disctinction between variables clear.


Javascript Variables : Declaration and Initialization

1. Javascript variables must be declared before being used in the program. It should be prefixed with the keyword var, though not mandatory.

2. Values can be assigned to variables after initialization, though not mandatory.If the variable is declared but not initialized its called as "undefined".

Example: Javascript - Variables

Note: Since extra whitespaces get ignored, users can create line breaks, indents or organise programs etc.


Javascript : Scope of Variables

1. Scope of the variable defines where the variables can be used or its visibility. Javascript Variables are of two types

Global : Global variables can be accessed by any Javascript on the document

Local : Local variables are declared within a function, they are private to the function and can be accessed from only within the function

Example: Javascript Variables - scope

Give it a TRY! » Note: The attribute defer is supported only for external Javascript files and not inline Javascript.


Javascript : Concatenation of variables

1. In order to concatenate variables the operator + is used. Addition is performed when both the operands are numbers.

2. Incase, the expressions involved are both numberic and string, then to perform the addition operation, numeric values are converted to string .

Example: Javascript Concatenation -

Give it a TRY! » Note: The attribute defer is supported only for external Javascript files and not inline Javascript.