Javascript Tutorial – Data Types and Variables

Data Types and Variables

Data Types

What is a Data type
It is made up of 2 words.
Data+Type
which means Type of Data.
Let’s understand first, what is Data.

Data means Information. Information about something. Let’s take an example of a Retail Store, So, when you go to a Retail Store like DMart or Wallmart or somewhere else, then when you pick some things and go to Cash Counter for Billing, then the guy at the Cash Counter scans the Items and get all the information about the products.

  • Name of the Products – Toor Daal, Parle G Biscuit,
  • Quantity of the Products – 1 Kg, 100 gm
  • Price of the Products – 200 rs, 30 rs

This is called data, and we can see that some data is in Text form and some data is in number form, and this is called data type.

Type of Data Types

There are 2 Types of Data types in Javascript

  1. Premitive Data type – Built in Data type in Javascript like Number, Boolean
  2. Non Premitive Data type – User defined data types like Objects, Arrays, functions

Type of Premitive Data Types

There are 7 types of Premitive Data types in Javascript

  1. Boolean
  2. BigInt
  3. Number
  4. Null
  5. String
  6. Symbol
  7. Undefined

Type of Non Premitive Data Types

  1. Object
  2. Arrays
  3. function

Variables

Now, We need to understand, what is variable.
So, We must know that the data, we were talking about or the information we were talking about, needs to be stored somewhere. So, all the information is stored at some location in data base.

We can understand this with a real time example, So, When you go to a Retail Store, and you need to buy anything, then those things should be stored somewhere or available somewhere. For Example

Toor Daal is available in the 3rd row of Grocery Section of Ground Floor.
Rice is available in the 4th row of Grocery Section of Ground Floor.

Now, when you ask a question, that what is 3rd row of Grocery Section or what is 4th row of Grocery Section, then I would this is the name of the location, where the Products are stored. Why do we provide some name to these location, and the answer is that it becomes easy to access the Products, If we know the name of the location where they are stored.

Same way, We need to store our data at some location in our database, and We need to give some name to that location and the name of that location is called Variable. It becomes very easy to access the data, If we store them in Variables.

So, We can say, Variable is the name of the location, where our data is stored, and we can access our data whenever we want using variable.

How to declare a Variable in Javascript

Generally, In other languages like Java or C, We need to mention datatype while declaring a Variable, something like this.

int a = 10;
String b = “Hello”

If we want to assign some different type of Values to these Variables, then that is not posible.

a = “QAPeddia”
b = 20;

This is going to give error.

But in Javascript,

  • We don’t need to mention data type while declaring a Variable.
  • We need to use some reserved keywords provided by Javascript for declaring a Variable.
  • We can change the data type of a Variable in run time as well in Javascript.
  • Javascript is called dynamically typed language because we don’t need to mention data type explicitly when we declare a variable in Javascript
  • Javascript is called Weakly typed language because we can change the data type of the variable during runtime.Offering popular women’s necklaces such as pendants, chokers and chain necklace. Shop for jewelry in a variety of metals and gemstones to suit any occasion

Keywords for Variable declaration

Generally 3 type of reserved Keywords are used to declare a Variable.

  • var
  • let
  • const

var

  • This is a Keyword was being used before ES6 version of Javascript.
  • This Keyword is used to declare varibales.
  • It is compatible with latest versions as well.
  • Values can be changed at run time.
  • Data type can be changed at run time.

let

  • This is a Keyword started getting used after ES6 version of Javascript.
  • This Keyword is used to declare varibales.
  • It is not compatible with previous versions.
  • Values can be changed at run time.
  • Data type can be changed at run time.

const

  • This Keyword is used to declare constant.
  • Values can not be changed at run time.
  • Data type can not be changed at run time.

Example

Let’s see, How to declare a variable for all the data types.

let a = null;
let b = 540;
let c = true;
let d = BigInt(“470”)
let e = “QAPeddia”
let f = Symbol(“This is a Symbol”)
let g = undefined

Console.log(a, b, c, d, e, f, g)

We can use var as well instead of let.

How to get the Data Type of a Variable

Now, the question comes, that we have used same keyword for all the variable, then how do we know the data type of a variable, then we have a operator for that.
How to get the data type of a Variable

Console.log(typeof d)

If we are declaring an undefined variable, then we don’t need to assign any value to it, It will automatically be undefined.

Changing the value of a variable.

b = 340;
c = false;
e = “Hello”

Console.log(a, b, c, d, e, f, g)

Changing the type of the variable.

b = “QAPeddia”;
c = 47;

Console.log(typeof b)
Console.log(typeof c)

Constant

const a = 10;
const b = “Hello”;

a = 20;
b = “QAPeddia”;

We can see that the values can not be changed for a Constant.

Problems with Var Keyword

  • Redeclaration was allowed.
  • Local variable with same name used to change the value of Global variable with same name.

These 2 problems could introduce bug in the system, which we can’t catch at compile time and it would be very difficult to catch a bug at run time.

So, let keyword was introduced in order to solve all these problems.

var a = 10;
var b = "QA Peddia";

console.log(a);
console.log(b);

var a = 5;

console.log(a);
let a = 10;
let b = "QA Peddia";

console.log(a);
console.log(b);

let a = 5;

console.log(a);
var a = 10;

console.log(a);

{
var a = 5;
console.log(a);
}

console.log(a);
let a = 10;

console.log(a);

{
let a = 5;
console.log(a);
}

console.log(a);

Identifiers

Identifier is the name assigned to any Variable, function or anything in any Programming language. There are some rules associated with Identifiers.

Rules for Identifiers

  • Names can contain letters, digits, underscores, and dollar signs.
  • Names must begin with a letter.
  • Names can begin with special characters as well.
  • Names can not begin with a number.
  • Names are case sensitive (y and Y are different variables).
  • Reserved words (like JavaScript keywords) cannot be used as names.

Summary

We have covered below topics in this session.

  1. Data Types
  2. Type of Data Types
  3. Variables
  4. How to declare a Variable in Javascript
  5. Keywords for Variable declaration
  6. How to get the Data Type of a Variable
  7. Changing the value of a variable.
  8. Changing the type of the variable.
  9. Constant
  10. Problems with Var Keyword
  11. Identifiers
  12. Rules for Identifiers

Leave a Comment