6. JavaScript Tutorial | Variables & Data Types

Share this post on:

yo what’s going on guys thumb buyer for tell you scold earnings and in this video tutorial I’m going to be understanding what are variables and data types in JavaScript so since this is a very big nerve level tutorial we’ll just take a little bit of theory on variables and we will see the data types and then we will jump to the programming part that is we will jump to the Visual Studio code so as you can see on the screen I have a little bit of jargon filled definitions for variables so these are the bookish definitions which you can find on the internet also so let me just quickly read through them and then we will see an explanation so variable provides us with the name storage that our programs can manipulate so this is definition number one another type of definition would be variables are used to store information to be referenced and manipulated in the computer program so pretty much similar to the first one just with a little bit of more words complex words actually and then in programming a variable is a value that can change hence actually the name variable variable basically means an entity that can variate or change and depending on the condition or the information passed to the program so after reading these three definitions it sounds pretty complex but of course it’s very easy concept that is variables and data types and if you are coming from any other programming language and if you have any other experience of any programming language pretty much the same concept is being applied over your of variables and data types with a little bit of difference so for the absolute big-nose to give your context let me just give you a scenario so on this right hand side what you can see is our basic diagram of a desktop so in a computer system what you have is a ram which is actually acting as your primary memory so whenever you execute a program or whenever you open any application or any software what happens is in the RAM some memory is being allocated okay so let’s say you have open your Chrome browser so then some memory will be allocated for the Chrome browser to run in the ramp similarly our JavaScript program whenever runs in the Chrome browser or whenever it is running it is basically existing in the RAM right so our RAM is basically divided into small small memory blocks and each of these memory blocks has some exodus symbol or some binary address which is pretty weird and long so you obviously cannot remember as a human being so it’s some hex address values also like F f0x and something like that so when you create a variable what happens is in the memory you allocate some portion of the memory and you give it a name okay so that’s why we have a named storage over here so what you do is we allocate this memory and we say okay I’m gonna name it a so even though it has an address which is very weird to remember we give an Elias name that is an extra name and we call it a so in JavaScript it goes something like this we have to see where E and let’s say we are storing a number five okay and then semicolon so in this memory block we have the value of five so we will talk about the syntax when we actually get into practical part but here’s what the scenario is happening in the behind the scenes in the memory okay now the next obvious question that you must be having is why variables why do we need variables right so the most simple answer to this is to store data that’s the most simple way I can answer you why you need variables now imagine a program wherein you’re taking values from users of a rectangle so you’re taking length and breadth and your program is calculating the area of rectangle so when you take those values of length and breadth you need to store them in certain variables because you want to perform some mathematical operations right you have a formula to calculate area so in that case you will require some variables now obviously as a human being you cannot remember each and every memory address rate so you cannot remember these numbers so these memory addresses are big numbers which only a machine can read so this variable helps us by giving it that memory block another name and then we can use this name to access that same memory block okay so this is a basic explanation of variables now let’s move on to the types of variables or data types of variables in JavaScript okay so now that you’ve understood the concept of variables every variable will have a type and basically type means what kind of data is being stored so variable where a can store a number let’s say it’s five it can probably store a string which will have some name ABC and it can store boolean and so on and so forth so of course every programming will have a set of data types which are unique to them so since we are dealing with JavaScript we have these many data types as you can see in the restructure so we have primitive data types which are three different types of primitive data types number string and boolean then we have some other data types or trivial data types which are actually primitive only just that they have one more name that is trivial or other types which is null and undefined so this is one two and three so we have numbers we have strings and we have boolean as primitive types again we have null and undefined also as pyramidal types but they are also called trivial or other types and lastly we have composite types which are complex types basically which consists of objects and arrays so we will talk about complex or composite types in further videos wherein I will have separate videos for each of these type right now we will ust deal with these three primitive types in the programming part so before moving to the visual code text editor that is the practical side these are two very important points that you need to understand for JavaScript specifically for JavaScript so let me just read it out and I will explain to you so javascript is loosely typed programming language and the second point says javascript is dynamically typed scripting language or programming language it’s one and the same thing because we are on the client-side scripting now remember these two keywords loosely and dynamically typed okay now let’s move on to the programming part and I will explain to you what is loosely and dynamically mean by showing examples and we will also see how to create variables and different variations of the primitive types as well okay so as you can see I have my text editor open that is visual studio code and this is our basic HTML document inside that we have our script tag but here we have declared certain variables so in JavaScript this is how you create a variable now if you’re coming from a C++ or Java background this might look a little bit weird to you right because in C++ and Java used to be like in x equals to 5 and something like that right we we already had a data type but here you can see there is no data type we are only using the keyword var so this is where that loosely typed keyword comes into picture that is GS is loosely typed what this line means is you don’t have to define the data type of the variable when you create the variable okay so you don’t have to tell what data type it is in so what happens is whatever the type of value you pass in the variable automatically the datatype will change and this is where the second definition or second point or second property of JavaScript comes into picture that is GS is dynamically typed which means whatever value you pass depending on the type of value the data type of the variable will change now here you can see we have three different types of variables so the first one is a number type because we are passing a number in it second one is a string because I’m passing a name then my sock palea that’s my name and the third one is a flag variable which is having a boolean value which can have two or false so I can change this to false also okay so these are the three basic variables that is the primitive types that we just saw in the previous digital blackboard so again the way you create a variable you type in where then you give the name and you can assign a value right there tself or you can just declare it and later on assign the value so this is just declaration and this is initialization and you can do both in one single line you can see this is that example wherein I’ve also declared a variable named num and I have initialized the value of 60 okay so now when you’re creating variables there arecertain rules to create variables that is to assign names to the variables okay so basically what I mean is there are certain naming conventions being followed which are known as identifiers so the rules for creating an identifier basically identifier is the name that you give to a variable in JavaScript are as follows so the name of the identifier should not be any predefined keyword so you cannot name your variable as where okay so I cannot say where where so this is going to give you an error you can see there is showing because where is a predefined javascript keyword so you cannot use that then the first character must be a letter or an underscore or a dollar sign it cannot be anything else so I cannot start with where at the rate so this is again going to be giving me an error however I can start off with underscore or a dollar sign and then the next character is maybe any letter or digit or number or underscore as you wish so you can have num1 or you can have num dal or something in between and lastly we Able’s are case-sensitive to Arnhem and if I create one more variable and if I see nu n where n is capital then this is different and this is different so this is what case-sensitive means so there are two different variables because n is capital so these are the naming conventions and now they are clear in your mind probably you can write them or note them down if you’re an absolute beginner or you can just take a screenshot so lastly what I’m going to do is as you can see the last statement of this script tag we’re in our JavaScript line is document dot writeln um so what I am doing is I am taking this num variable whose value is 16 and I’m just printing it on the web browser so let’s try to run this default dot HTML okay so as you can see I just opened that document I am NOT going live as of now I’m not using the live server plug-in because I just wanted to show you this printing and then there is another function which you can use which I just want to introduce over here is the alert function so inside this alert I am just going to write the num and I know I have not talked about functions but just type along with me I’m going to comment this document dot write statement save this and when I run this there you go you can see you got a pop-up message saying this piece is 16 the 16 is this number variable I’m just going to see ok so this is what this function does so I just wanted to introduce this so that in further videos when you use it you feel comfortable so one more thing that I wanted to show is as I mentioned javascript is dynamically typed which means the data type of the variable changes depending upon what value you pass so here you can see this num is storing 16 that’s why it is a number so what I’m going to do is add this line I’m going to say num is equal to and I’m gonna say ton my which is my name so now again I’m going to just pop up this message in the alert just save this and refresh this and they were now distinct on mine so this means that initially our num variable was a number but at this line because of course the code runs line by line at this line the num variable change its data type to string because we stored a string in it so this is what is meant by JavaScript is dynamically typed and of course you know what is javascript is loosely typed right because we don’t have to specify the datatype initially we don’t have to say int this will give you an error so it doesn’t work in JavaScript like that so J’s is loosely type or another way to see it is this is not strictly typed so whenever you see a programming language is strictly typed it means that the datatype or whatever type of data is being stored has to be specified before so that’s it for this video guys his was just a basic fundamentals on JavaScript variables and datatypes and I hope you have a good idea about the features that this javascript is dynamically and loosely typed so thanks for watching guys if you like this video please give it a thumbs up let me know in the comments how this video was buu share it with your friends and if you have entered subscribe on this channel make sure to subscribe so t at you get notified whenever I upload a new video so thanks for watching see you guys in the next video peace.

Share this post on:

Leave a Reply