JavaScript Variables & Data Types

In this tutorial post we will study and understand the concept of Variables & Data Types in JavaScript.

Variables –

Following are some basic definitions of variables in a typical programming language context.

  • A variable provides us with a named storage that our programs can manipulate. It is the basic unit of storage in a program.
  • Variables are used to store information to be referenced & manipulated in a computer program.
  • In programming, a variable is a value that can be changed depending on the conditions or information being passed to the program.
Data Types –

Datatypes are basically type of data that can be used and manipulated in a program. For e.g. numbers(1, 2, 5,5), string(“hello Simple Snippets”, “JS”), boolean(true, false) etc.

Following are the different Data Types in JavaScript –

javascript data types - primitive and non primitive

JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript.(as shown in the diagram above)

  1. Primitive data type
  2. Non-primitive (reference) data type
JavaScript Primitive Data Types –
Data TypeDescription
Stringrepresents sequence of characters e.g. “hello”
Numberrepresents numeric values e.g. 100
Booleanrepresents boolean value either false or true
Undefinedrepresents undefined value
Nullrepresents null i.e. no value at all
JavaScript Strings –

A string (or a text string) is a series of characters like “Simple Snippets”. Strings are written with quotes. You can use single or double quotes:

var carName = "Mercedes";   // Using double quotes
var carName = 'BMW';   // Using single quotes

You can use quotes inside a string, as long as they don’t match the quotes surrounding the string:

var answer = "It's alright";             // Single quote inside double quotes
var answer = "He is called 'Mark'";    // Single quotes inside double quotes
var answer = 'He is called "Mark"';    // Double quotes inside single quotes
JavaScript Numbers –

JavaScript has only one type of numbers. Numbers can be written with, or without decimals:

var x1 = 34.00;     // Written with decimals
var x2 = 34;        // Written without decimals

Extra large or extra small numbers can be written with scientific (exponential) notation:

var y = 123e5;      // 12300000
var z = 123e-5;     // 0.00123
JavaScript Booleans –

Booleans can only have two values: true or false. Booleans are often used in conditional testing.

var flag1 = true;
var flag2 = false;
JavaScript Non-Primitive/Composite Data Types –
Data TypeDescription
Objectrepresents instance through which we can access members
Arrayrepresents group of similar values

We will learn more about these in future tutorial articles.

Creating Variable in JavaScript –
var length = 5;   // Number
var lastName = "Simple Snippets";   // String
var flag = true;   // boolean

In the example above, we created 3 primitive variables and also instantiated them with their individual values.

JavaScript unlike C++ or Java Programming has 2 unique characteristics in context with variables and data types

  1. JavaScript is LOOSELY/WEAKLY TYPED programming language.
  2. JavaScript is DYNAMICALLY TYPED programming language.
1. JavaScript is  LOOSELY/WEAKLY TYPED:

Javascript is known as untyped/loosely/weakly typed language. This means that we do not have to specify the data type in advance unlike other languages like C++, Java C# etc.

Example –

var x;
x = 5;

In the above example we can see that we did not have to specify any data type for the variable x in advance.

2. JavaScript is DYNAMICALLY TYPED :

Javascript is known as dynamically typed language. This means, that once a variable is created in javascript using the keyword var, we can store any type of value in this variable supported by javascript.

Example –

// creating variable to store a number
var num = 5;

// store string in the variable num
num = "Simple Snippets";

In this example above, you can see that the data type of the variable num changes from number to string as we pass string data. This it is flexible in nature.

Variable Scope in Javascript –

Scope of a variable is the part of the program from where the variable may directly be accessible.

In JavaScript, there are two types of scopes:

  1. Global Scope Scope outside the outermost function(we will discuss functions in detail in other article) attached to Window.
  2. Local Scope Inside the function being executed.

Let’s look at the code below. We have a global variable defined in first line in global scope. Then we have a local variable defined inside the function fun().

<script type = "text/javascript"> 
var globalVar = "This is a global variable"; 
  
function fun() { 
  var localVar = "This is a local variable"; 
  
  console.log(globalVar); 
  console.log(localVar); 
} 
fun();
console.log(localVar);  

< /script>

Output:

This is a global variable
This is a local variable
Uncaught reference error: localVar is not defined

We will have more discussion on individual Data Types (especially the Composite/Non-Primitive types) in further tutorials.

Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *