.

Variables in JavaScript Assignment Help

5.1 Data types

Java script support built-in and composite data types:

variables img1

5.2 Variables

Variables are named entity in java script environment. The entity holds values. The values are retrieved from the entity using the name. Variables are declared within < script>…< /script> tags using the keyword var.


Variables in JavaScript Assignment Help Through Online Tutoring and Guided Sessions at MyAssignmentHelp


Declaration:

var variable_name;

Example:

{`
< script type = "text/javascript">
var num1;
var num2;
< /script>
(or)
< script type = "text/javascript">
        var num1,num2;
        < /script>
        `}

Initialization:

Assigning values to variables is known as initialization. The assigned values decide the data type of the variable. Unlike high level languages, no need to explicitly convey the data type of variables. Variables have the flexibility to hold any data type dynamically.

Example

variables img2

5.3 JavaScript Variable Scope

Scope of variables decides the boundary up to where the variables are visible.

It has two variables base on scope such as,

  • Local
  • Global

Global variables

It is visible thorough out the java script environment.

Local variables

These variables are expired when it reaches out of the block where it is declared.

Example

variables img3

Explanation

The variable ‘a’ declared as global and ‘b’ as local variables. The variables ‘a’ and ‘b’ can be accessed with in the function. The variables display output as 10 and 20. The variable ‘b’ cannot be accessed outside the function. While trying to displaying the values of the variables ‘a’ and ‘b’ it will only the display output as the value of ‘10’.

5.4 JavaScript Variable Names

Protocols for giving names for variables:

  • Reserved words are not be variable names
  • Valid variable name should starts with alphabet letters not numbers or restricted characters.
  • Variable name is the combination of alphabets, numbers and underscore.
  • Java Script considers different meaning for capital letters and small letters. In Java script environment capital letters and small case are considered different. For example, variable names pi ,PI,Pi,pI are different.

5.5 JavaScript Reserved Words

variables img4
.