JavaScript - SPLessons

JavaScript Variables

Chapter 4

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JavaScript Variables

JavaScript Variables

shape Introduction

A variable contains a value referring to the data. JavaScript Variables can be used to store, retrieve and manipulate the values present in the code. So, they must be given meaningful names to make user easily understand the function of the code. The present chapter “JavaScript Variables” gives users an overview of variable types and illustrates how to define a variable.

Declaring the variables

Initially, users need to create a variable in the memory by declaring it i.e., the value must be placed inside the RAM. So to define a variable, var keyword should be used followed by the variable. For example,

var message;

The above variable message does not hold any value, but if assigned, it can store any value. The value contained inside the message is ‘undefined’, since no value is given explicitly.

shape Examples

Following examples illustrate the declaration of variables.

var age; //age variable declared
var age,date,amount; // age,date,amount variables are created
var age=24,date="16/6/2020", amount=20000; //variable age is assigned value 24,date assigned 16/6/2020 value and amount given 20000 value.

Identifiers

shape Description

Unique names are given to the JavaScript Variables, which are known as Identifiers. The names of the variables can be user-defined. Identifiers can be short or long and define the function to be done.

Keywords

Keywords are the words that have pre-defined meaning given by the JavaScript Interpreter. Identifiers or property names cannot be Keywords as they are reserved.

break case catch continue with default
delete do else finally for function
if in instanceof new return switch
this throw try typeof var void
while

Reserved Words

Reserved words cannot be used as identifiers or property names. Even though reserved words don’t have any specific usage in JavaScript, they are reserved for future use.

abstract boolean byte char class const
debugger double enum export extends final
float goto int interface implements import
long native package private protected public
short static super synchronized throws transient
volatile

shape Examples

Below example shows how to define a variable and get the value as the output.

<html>
	<head>
	<title>First Javascript Page</title>
	</head>
 
	<body>
		<h1 style = "color: blue">JavaScript Variables</h1>
		<h2 style = "color: green"> Welcome to SPLessons</h2>
		<p style="color: red">In this example, i is defined as a variable.
		Then, i is assigned the value of 5</p>
		<p id="splessons" > </p>
		 
		<script>
			//defining variable i
			var i;
			//assigning value 500 to i
			i=500;
			document.getElementById("splessons").innerHTML = i;
		</script>
	 
	</body>
</html> 

The output appears as below.

Summary

shape Key Points

  • JavaScript Variables contain values.
  • Identifiers are user-defined names.
  • Variable names should start with letters,_ or $.
  • Variables are case-sensitive.