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.
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.
There are 2 Types of Data types in Javascript
There are 7 types of Premitive Data types in Javascript
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.
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,
Generally 3 type of reserved Keywords are used to declare a Variable.
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.
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.
b = 340;
c = false;
e = “Hello”
Console.log(a, b, c, d, e, f, g)
b = “QAPeddia”;
c = 47;
Console.log(typeof b)
Console.log(typeof c)
const a = 10;
const b = “Hello”;
a = 20;
b = “QAPeddia”;
We can see that the values can not be changed for a Constant.
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);
Identifier is the name assigned to any Variable, function or anything in any Programming language. There are some rules associated with Identifiers.
We have covered below topics in this session.
Objects, Classes and Constructor in Javascript Classes and Objects A class in any programming language…
We will start from object oriented programming basics like What are different concepts in Object…
Why we can not over ride a Constructor This is a very important question, that,…
What is Arrow Function In the last session, We had discussed about Anonymous Function. Anonymous…
What is Anonymous Function Anonymous Function is a type of function which has no name…
What is Map, Reduce and Filter Map Filter and Reduce are higher order methods for…