JavaScript
Topperworld.in
Variables & DataTypes
❖ JavaScript Variable
• A JavaScript variable is simply a name of storage location.
• There are two types of variables in JavaScript : local variable and global
variable.
There are some rules while declaring a JavaScript variable (also known as
identifiers).
1) Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ )
sign.
2) After first letter we can use digits (0 to 9), for example value1.
3) JavaScript variables are case sensitive, for example x and X are different
variables.
Correct JavaScript variables
var x = 10;
var _value="sonoo";
Incorrect JavaScript variables
var 123=30;
var *aa=320;
©Topperworld
JavaScript
Example of JavaScript variable
Let’s see a simple example of JavaScript variable.
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
Output:
30
❖ Types of JavaScript Variable :
• Global Variable
• Local Variable
• JavaScript local variable
A JavaScript local variable is declared inside block or function. It is accessible
within the function or block only.
©Topperworld
JavaScript
For example:
<script>
function abc(){
var x=10;//local variable
</script>
Or,
<script>
If(10<13){
var y=20;//JavaScript local variable
</script>
• JavaScript global variable
A JavaScript global variable is accessible from any function.
A variable i.e. declared outside the function or declared with window object
is known as global variable.
©Topperworld
JavaScript
For example:
<script>
var data=200;//gloabal variable
function a(){
document.writeln(data);
function b(){
document.writeln(data);
a();//calling JavaScript function
b();
</script>
❖ Declaring JavaScript global variable within function
To declare JavaScript global variables inside function, you need to use window
object.
For example:
window.value=90;
Now it can be declared inside any function and can be accessed from any
function.
©Topperworld
JavaScript
For example:
function m(){
window.value=100;
//declaring global variable by window object
function n(){
alert(window.value);
//accessing global variable from other function
❖ Internals of global variable in JavaScript
When you declare a variable outside the function, it is added in the window
object internally. You can access it through window object also.
For example:
var value=50;
function a(){
alert(window.value);
//accessing global variable
©Topperworld
JavaScript
DataTypes
❖ Data Types
• JavaScript is a dynamically typed (also called loosely typed) scripting
language.
• In JavaScript, variables can receive different data types over time.
• The latest ECMAScript standard defines eight data types Out of which
seven data types are Primitive(predefined) and one complex or Non-
Primitive.
➢ Primitive Data Types
The predefined data types provided by JavaScript language are known as
primitive data types.
Primitive data types are also known as in-built data types.
◆ Number: JavaScript numbers are always stored in double-precision
64-bit binary format IEEE 754. Unlike other programming languages,
you don’t need int, float, etc to declare different numeric values.
◆ String: JavaScript Strings are similar to sentences. They are made up
of a list of characters, which is essentially just an “array of characters,
like “Hello TopperWorld” etc.
◆ Boolean: Represent a logical entity and can have two values: true or
false.
©Topperworld
JavaScript
◆ Null: This type has only one value that is null.
◆ Undefined: A variable that has not been assigned a value
is undefined.
◆ Symbol: Symbols return unique identifiers that can be used to add
unique property keys to an object that won’t collide with keys of any
other code that might add to the object.
◆ BigInt: BigInt is a built-in object in JavaScript that provides a way to
represent whole numbers larger than 253-1.
➢ Non-Primitive Data Types
The data types that are derived from primitive data types of the JavaScript
language are known as non-primitive data types.
It is also known as derived data types or reference data types.
◆ Object: It is the most important data type and forms the building
blocks for modern JavaScript.
◆ Array: represents group of similar values.
◆ RegExp: represents regular expression.
We will learns all these data types in detail later…
©Topperworld