Transcript for:
Introduction to JavaScript Programming

imagine knowing a programming language using which you can build any type of application web application front-end back-end both mobile application desktop application and even for machine learning and that language i'm talking about is javascript so using javascript you can build all these applications [Music] welcome back aliens my name is evan reddy and let's get started so we are talking about javascript and finally i'm starting a series on javascript so first of all what is javascript now javascript is a programming language which you can use to develop any kind of application which we named before but then it didn't start in that way in 1995 when javascript was released it was for a different purpose it has a different history again we'll talk about the history of javascript so that we can predict predict the future and we'll discuss that in a separate video but let's talk about 1995. when javascript came into existence it was mainly for the web front-end example in 1993 when websites were getting famous everyone wanted to have their own website but the only way you can interact with the website is one way so basically you request for a page and watch you get a static content of course you can design it but then you do you can't interact with that application every time if you want to move between pages you have to use hyperlinks so basically that website was not interactive and if you want to keep your audience engaged you have to make it interactive so netscape came up with this programming language called javascript with the help of sun microsystem and they released javascript in their netscape navigator 2. so that's awesome right we got a programming language but that was only for the web front end just to make your web application interactive and for many years it was that way i mean the moment you think about javascript the only only use case you can find is building the front end of the web application but then things got changed now you can use javascript anywhere the reason is javascript was easy and we have so many people working on this language so they thought hey instead of using javascript only for web application can we use it for building the back end of web as well maybe we can use it for mobile applications we can use it for desktop application or maybe we can use it for machine learning and that's where they started using javascript everywhere they were able to do it because of the engine so every browser will be having a javascript engine so the reason you are able to run the application of course we'll see that in the practical but you can run any javascript code on the browsers because it has a engine just take that engine make sure that engine runs on the hardware or the os which you have basically you can run javascript on your os now or on your machine right so just to give you a hint that is possible with the help of node.js so node.js is a runtime environment imagine a place where you can run javascript basically if you have that stuff anywhere maybe a mobile application desktop application machine learning you can run it that's the power of javascript now this course is not designed in a normal fashion or the traditional fashion so the moment you talk about javascript everyone want to learn about the html dom elements and how do you change the data on the html page because that's how javascript came into existence right now that's not the reason why i'm making this playlist for me i want to treat javascript as a programming language not only for the web front end okay see there's no harm in learning javascript for the web and then move towards other aspects but it's tricky right once you move from front end to back end you may not be comfortable so i will start this series running javascript directly on the machine okay so that we will not learn javascript only for web but in a proper way so that you will understand javascript from start to end of course we will be using that for front end in between we will be using for let's say we will talk about how can we use it for other purpose as well but the idea is to learn javascript from start to end okay now coming back to javascript javascript is actually one of the most famous programming language nowadays and if you go to google and search for top five programming language doesn't matter which article i'm talking about check the first 10 links they will mention javascript in top five someone someone might say one or two or three or four or five you will see javascript there okay i've not done that research but i'm quite sure javascript is that famous why is because you can make applications on any platform right and most of the websites in fact as for wikipedia 97 percent of the websites are using javascript for their client side so you don't have an option right so we have a different history we have some awesome facts okay so in this series we also talk about some facts examples let me give you one fact uh if you know javascript and if you have been working on javascript so i want to know in the comment section who owns the trademark for javascript okay pause the video comment your answer so let me give you the answer it's oracle okay so there is a huge history behind javascript and it's interesting so we'll talk about that as well i hope you are excited for this series uh i want to make sure that you learn from start to end but you have to make sure that you also practice it's a programming language right you you can't just look at the video and you can you will remember everything you have to practice practice makes you perfect okay so every time you watch a video do it on your machine i would also suggest that you change the code do some experiments and that's how you go now in the first video we have talked about some great things about javascript right it's one of the best language available it can be used to make any kind of application doesn't matter is it web application mobile application or desktop application and you can also use this for machine learning so awesome right and you are waiting to write your first code and so we don't want to wait right i don't want to invest your time we just want to write our first code here but the problem is which code to write and why do we even write a code see the ultimate aim here is to automate things the ultimate aim here is to solve the real world issues with the help of virtual world solutions you wanted to text someone so we got a whatsapp you wanted to make a video which will be seen by everyone in the world we got youtube right so you can use software to build solutions and the first step to do that is to write a code right you need to write some lines of code and we will start with one of the most basic and most important line of code for any programming language so doesn't matter which language you start with javascript python java c c plus plus the first thing you do is you print hello world because if that works most of the code should work that's the idea behind learning hello world now the question arise where you will write this code where you will run this code so basically your computer understands only one language which is binary right so whatever you write has to be converted into binary and someone will do that it might be a compiler it might be an interpreter so these two words are very famous in the world of programming where you have a compiler or you have an interpreter now some languages are compiled language some languages are interpreted of course again we'll talk about those two words in detail later as of now remember whatever you write need to be converted into a machine code and for that we need some software let's say let's generalize it as a software in the world of javascript we can call it as javascript engine right so if your machine has a javascript engine it will convert your javascript code into a machine code so we can use that now the questionnaire is variable type your code so we will be using one of the world best editor not exactly but let's say let's try notepad so let's use notepad here and let's write our code so what we want to achieve is to print hello world right the way you can do that is by simply saying hello world that's it right so we are writing this code let me just save that on a desktop itself i will name this as uh you can name anything i will just say first dot js now dot js is an extension which is used for javascript files as you change your language in in java we say dot java in c we say dot c in c plus plus we said dot cpp so every language have their own extensions in the same way for javascript we have dot js that's it we have saved this file right but then where you will run this now that's tricky and will this work uh see let's let's start with the second problem it will not work actually if you want to print something with the help of engine you have to instruct it to print okay and the way you can do that is by using a thing called as console.log now what exactly console.log is and why there are two words why there's a dot in between we'll talk about that later okay something we have to we'll see later okay that's something you will you will hear a lot in this series but that's how the thing works right you can't learn each and every word in the first video so we'll go step by step uh and then you have to put that in double double quotes again why we'll discuss that later here just want to see the output and we are trying to understand how can you run this okay we'll talk about these things in detail later so as of now i have a statement which i want to execute and i want to see the output so this this thing here will print hello world but again the problem is how do you run this so let me save this now in this machine do i have a javascript engine so maybe in this machine i have used it but do you have a javascript engine in your machine what if i say yes what if i say your machine has a javascript engine but then you have not installed it right see if you have a browser example chrome browser microsoft edge or opa doesn't matter which famous browser i'm talking about most of the browser has a inbuilt javascript engine okay there are some browsers which are which don't have and nobody uses those browsers okay so we will be using a browser to run this code so maybe i will not run the entire file i just want to execute this statement right so what we can do is we can open any browser which you like so let me open chrome here now in chrome i don't know if you have observed it so if you right click and if you go to inspect you will get multiple developer options so you can see there are so many options here this is specifically developers web designers they use this a lot for seeing the output to debug the application to understand what is happening behind the scene so we will be using the same thing and we will be jumping to console you see that in the code also we have mentioned console.log so we are logging so we are writing here on this console uh let me increase the font size so that you can see what is happening so every browser by default has an engine right so we can simply say what we want to do we want to say console.log so it will also give you some hints uh because it is advanced since you are learning this in 2021 or later we have so many options we have so many intelligence in browsers or in most of the softwares so it will help you to code so in this log i will say we are writing the same code okay which we have done in the notepad and then once you write a code you just have to say enter and you can see we got the output so what you see on the next line is an output okay what else we can do here we can also add two numbers we can say two plus two enter we got four so basically you can this is a place where you can write the entire application so whatever you do you want to do some complex things you want to do some simple things everything can be done here okay but that's the thing whenever you make application irrespective is it web front-end is it a mobile application or the extra application it will not be done with only one line of code right so here writing one line of code and you're getting the output it's not that we can't reuse those code we can reuse the data we can reuse the statement by copy pasting it but this is not the best place to write a code one of the place where javascript is getting used a lot is to make a web front end okay so maybe if you have done some other course on javascript you might have seen that you can write a javascript code inside html and i hope you know html css that's a prerequisite for this course not initially but later once we start with the making web pages it will be important as of now it's okay even if you don't know html and css so the important thing is we are writing a code here right but this is not the best place to write a code so what we can do is we can create html file and we can write a code there but if you remember the first video i have mentioned i'm not going away where i will be teaching you javascript for web pages it's a good thing but not my way of learning a programming language right so we'll learn this language on a console writing the javascript as it is without html and css so basically we don't want to run this on the browser because browser needs so we have a console here then otherwise we have to use html to see the output and those stuff i don't even want to touch browser basically so what if i can run this on a console on a terminal example command prompt so i will open command prompt i want to see the output here how will you do that can you run javascript on your machine see javascript has a different history right javascript one was initially built for web so you know few years back or 10 years back they were only running javascript on browsers but then something changed right we got a new thing where you can run javascript on your os right and that is possible with the help of a software called node.js so there is a software so in this machine i already have node.js so i will simply type node iphone version so i will let me just check do i have node on this machine and the answer is yes so using this software i can run the code so i will say city desktop now on desktop i have this file right so let me just say dir do i have this file yes i have first dot js let me run this i will say node and i will say first dot js and if i say enter who we got hello world now we don't have to depend upon one line of code you have a notepad here you can write any number of lines and it will work okay and it will execute in sequence and you will see the output doesn't matter is it one line output then output or even errors right so you will see everything here but the problem is on your machine you don't have node also we don't want to use notepad we want to use some advanced editors where you can actually see colors you can actually understand what is happening editor should have option to debug the application and those stuff and which editor we are going to use we are going to use one of the best retards of now which is vs code you can use any ide which you prefer maybe you can use atom sublime those editors actually you can also use advanced office like intellij we will be using vs code which works perfectly and it's one of the most famous editors as of now so we'll use that okay so it's time to get those two softwares so we need vs code and we also need node so node is a runtime environment so basically it's an environment where you can run your code and you also need vs code where you will type your code right so what we will do is first let's download node here so we'll say node.js download and you can go to the official website where you can get the setup and it's always better to get it from the official website okay now we you can see we have two different versions we can go for lts which is long term support we can also go for the current one now the only drawback of current one is there might be some bugs and the version which you are using here might not be getting the long term support so it's good for development purpose and goods for your own machine but if you want to use it for the production uh don't use that so here we will be using this lts version so let's download the windows one because this is a windows machine now if you're using mac or linux based on that you can select your versions for windows as well you can go for 32-bit or 64-bit now this is a 64-bit machine so i'll be going for 64. now it might take some time to download so by the time it gets downloaded we also need one more software which is the vs code so i will search for vs code and again official website let's go there now based on your system it will automatically detect your os versions so since we are working on windows you can select that if it is not selecting by default then you have to choose but then we know it's windows so let's download this table version click on that and you can download this table one okay so if i go to my downloads you can see we have both the software install both the software downloaded it's time to install them and we'll let's start with node.js so even before installing node.js let me just verify from my command prompt if do do i have node.js here so it's a node version you can see we don't find anything so what i will do here is i will go back to my node and downloads i will install node now so as i mentioned node is a runtime environment so if you don't have it you can't run javascript on your machine so we'll say next you don't have a choice right you have to accept the agreement okay click on next install not difficult step you just have to say next next next okay done so you can see node.js got installed click on finish now how do you verify it is working so again we'll open command prompt and in this we'll type node version typo there node version you can see we got the node version installed so we are going for node 14 because it's a lts version you can also go for the current one which should work let me just go also install vs code so i will double click here again steps are not difficult you just have to say i accept the agreement say next next next do i want a desktop icon yes click next install it might take some time okay when you open videos code for the first time there's not a screen you will see what you will see here is a welcome page because earlier also i did that and i removed the welcome page and then you will get an option of opening a folder so what we'll be doing here is we'll open our desktop and on this desktop let's create a folder and let's name that folder as javascript so we'll put all our java files here okay so we've got a folder let's open that folder so we'll say open folder and we'll navigate to desktop and let's select js folder click on select folder okay so you've got a folder right now where you will write a code so on the left hand side you will see this is a place where you will have all the files this is a place a center page where you can type your code and if you want to see the output you can arrange that on the right hand side or at the bottom that's your choice but since we want to have a bigger vertical space we can have output on the right hand side okay but as of now i just want to create a simple file because i just want to print hello world the way you can do that is by creating a new file and we'll name this file as first.js to do that you just have to click this file plus button and it will give you a file and this is where you will write your first code so we'll say console dot log and in bracket you say hello world that's it so once you like this code where do we run this so basically we'll run this on a terminal in fact you can also do that on the command prompt which we did earlier in the last video this time what we'll do is we'll click on this terminal and say new terminal imagine this as a command prompt but this is actually a powershell so what we will do here is we'll say node we'll also check do we have node available here okay node is available here so what i will do is i will say node and you have to mention the file name which is first dot js so node space your file name and you will get the output as hello world is that simple right let's say you have already learned java c c plus plus any of the language you can see an option of run button right what if you want to run that with a button here so vs code is actually a very awesome ide which provides you so many things it's not like a normal notepad when you just try to type a code it provides you everything you can write a code you can run your code you can debug your code you can add extensions which will help you in the coding debugging in all the aspects so you can see on the left hand side if you come here this option of extensions so here we'll search for code runner this will help you to run your code there are tons of extensions available to to make it work you can just install code runner click on install and done close this and you can see we will get this simple button which is play button here and let's run this how do you run this so go back here and click on run but this time it will go into output uh tab not on the terminal tab and in the output you can see we got hello world and every time you make a change now so let's say i just want to add two exclamation mark there you just have to click on this play button you will get the output and you can also get this screen on the right hand side so we can simply say right click and you can say move panel to right and now you can have this output on the right hand side so that you will get a bigger space for the code so in your machine you will be having vs code and node.js so we need vs code where you will write a code where you also want to see the output and it will be helpful for debug as well we will be using node.js to run the code right we need some environment so node.js has a javascript engine using which you can run the code so we'll talk more about that javascript engine later as of now we just want to get started with the code right see we have also done the hello world but not on vs code i guess so what we will do in this video is we'll start with the first and most important concept in any programming language actually which is variables see first of all why do we need variables if you think about this world if you think about the information world or internet world everything is about information right in fact we also say this is a information age so the most important thing in it or the computers is information so whatever we do we do it for the information you want to get the input you want to show the output you want to process data it's all about data right now the questionnaire is where you will store data of course use with the help of programming you can process data but even for processing first you have to store your data somewhere and after processing you will store your data somewhere so we need to store our data somewhere and that somewhere is variables the only tricky point is when you want to store your data for a temporary purpose we will be using variables if you want to store your data for a permanent use we have a different options for it currently we are focusing how do you store your data in a code so we can do that with the help of variables so example let's say i have a data here so let's say i want to perform some operations before doing that so we'll add two numbers here first so i also want to print them so what i will do is i will say console.log the same thing we've done for uh hello world here what we'll do is we'll say two plus two now of course if you run this code you will get the output right so let's save this file as well so we are doing the same file which we started which is first dot js which is available on desktop okay so let me just run this of course you have two options of running this we can write our node command or we have installed this extension called code runner let's use that so when you click on run you can see we got the output as four okay this looks cool right but then i want to store this data somewhere so that i can use it example when i want to use it again i will do a calculation here and here i want to add two numbers one is a new value of course let's say i want to add three but i want to add this three with the previous operation maybe you have a choice let's say the previous operation is two plus two and then you will say three but don't you think you are doing the same thing again example when you run this code of course you will get four you will get seven four because of the first operation seven because of the second operation but don't you think we are doing it again so basically if this takes some time even this will take some time so what if you do this addition and you store it somewhere so let's do that what i will do here is before console.log i want to achieve this operation i also want to store it somewhere now where to store it so we have a concept of variables and variables has a name so let's use a variable name i will store this number in num so let's say num equal to 2 plus 2 so whatever you add here which is 2 plus 2 will be stored on this variable or inside this variable so this looks cool right and then i can just use instead of printing 2 plus 2 i can print num here so imagine this num as a box and inside that box you have a value now which is 4 so this is a expression which will evaluate and the value which is 4 will be stored inside num and then when you print num it will print the value of that box so box is num and what you get out is the value which is four okay so now we can use the same for here right but before that let me just run this to check if everything is running so whenever you do small changes make sure you run it to check what is happening so let's run this code okay at least we have not got the error we got the same output because we have not changed the operation yet the only change is this time you are fetching the value from a variable so the first for which you got here is coming from this num okay now i can actually reuse and that's the power right so once you have a data in the variable you can use the same data multiple times we are doing it here we are doing it here and you can use it again not just for printing the value you can also use it for addition which we are doing here you can do all the operations the data will be there okay how long the data will be there is it the right way of getting a variable i will talk about that as well but as of now it's running right see the most important thing is when you learn things for the first time so first let's understand what it is and then we'll see how we can use it and then we'll understand what is happening behind this scene this is not a time to understand what is happening behind the scene right so let's go with the flow we have a variable imagine a box you have a value you're printing it here and you're adding it here but is it the right way of creating a variable see if you want to create a variable in javascript you have an option of just by saying a variable name you can use it by chance if you are coming from other language let's say c c plus plus java or any language which needs you to declare variables that's right in some languages we have to declare them first here we're not doing that we are simply saying num but this is not a right way of defining a variable it is allowed here but then with every new version you get new features right so this was an old feature so if you want to get a variable say let so say let num equal to whatever value you want to assign so basically this let is a keyword using which you can create a variable there is other option as well but we'll see that later so we have let and then variable name value assignment and you don't have to always create an operation or create an expression to assign the value you can also directly send the value if you want so you can actually say if you have a value with you you can directly assign okay so this makes sense right you can actually assign a number so this here is a number right what if you want to store something else maybe you want to store a name so what i will do here is let me just remove this part and let me just go with a name here so i will select so every time you create a variable you have to select i will select and here i will say name is equal to okay now the problem is name is a inbuilt keyword so let's use something else let's say let's say user and this is important make sure that you always name your variable properly maybe you can also use a bc for a smaller code it works but then the moment you have lengthy code it will be difficult to guess what you're trying to do so don't use abc something like that always use proper names maybe you can also say username right so let's send this username i want to store my name so the way you can do that is by saying you can mention a name here so my name is naveen so i will simply say let username is equal to naveen and let's try to print this i will come back here i will say console.log and i will say username what do you think will it work it should right i mean everything is working as of now so let's run this code and let's see what happens and the moment you run this code oh this is your first error congratulations this is something if you're doing programming for the first time this is something you'll be doing in your lifetime right so as a programmer you write code and you face errors okay uh i don't want to scare you here but we have a variable username and then we are trying to assign a name and we got an error see what this editor says it says naveen is not defined that's where i am defined here see the problem is if you remember we have talked about this whenever you write a code that code has to be converted into a machine code because your computer understand only one language which is machine language so your engine which we are talking about will try to convert this code into machine code right so it will start in this way so it will say okay let let is what it's a keyword right so it knows what that means num okay it knows whenever you have a led keyword after that when you have num it's a variable name so javascript knows about these things equal to is in inbuilt what is the number right so of course we have five numbers uh then we have console again this is in build log is in build so everything which you can see on the screen so even this let is known to the javascript this username is a variable name so it will accept it the moment you say naveen it got confused it is confused is it a variable name is it something which which a user has defined this so there are so many things which is defined by the javascript and there are so many things which you can define example num is my creation username is my creation what about console.log it is already defined inside your inside your javascript engine the moment you assign navin it got confused is it a variable name so we have to say hey don't treat this is a variable name treat is as a string something which i want to assign example 4 is well defined right we have a finite number but strings it can be anything right it can be a lengthy name example my full name daddy palin kumar predi okay i will not repeat that but it can be anything so when you want to assign a string always use double quotes so text string it should be inside a double quote now you have an option you can use double quote or you can use single quote okay it's your choice javascript says you can use both and that's why javascript is a flexible language in that case but yeah if you are using a double quote at the start don't use single quote at the end okay so either you single quote or double quote on both the side and now it should run because i'm expecting it should be giving an output now so if i scroll down you can see it worked we got four and we got naveen so that's how you store a string or the text use single quote or double quote now why we have two options example let's say i want to store navin's phone on the wings account so in that case i will say so let's say i will not put any single quote adobe code as of now let me just write right right i will say naveen's phone or the means account so i want this to be treated as a text or the string right so what do you think what should i do should i use single quote or double quote here see the problem is if i use single quote we already have single code inside a text okay so if i put single quote there will be a confusion this single quote means opening a string this single quote was supposed to be printed as it is navin's phone right donovan's account i don't want this to be treated as the string context or string thing so what we can do is in this case we can use double quotes because i just want to avoid that confusion so we can use double quote when do you single quote now let's say you have double quote inside your text we will be using single quote or in general whenever you get a chance use single quote it makes much more sense okay and there's one more thing if you work with different languages example c c plus plus java they have double quotes so if you if you know multiple languages you might have a tendency to write double quotes and that's that is completely fine okay so now we know how to work with two different types we have used numbers we have used a string now likewise we have multiple type of data of course we'll talk about that in the next video or different type of data but we can create any variable and we can assign any type of values there now why they are called as variables is because we can change the value right so when you talk about variables what's something you can imagine variable means it can change in the same way if you have a num here we can change the value of it so let's say i don't want num to be 4 anymore i want it to be 9 we can do that so let's say i will print console so let me print num and let's see what the output is so if you run this code uh you can see we got four because of the first line or the first console then we got nine because of this string or this this thing and then we got 9 because of the last code here but the question arise why we are not using let second time it's because this time when you use num for the first time you are defining or you're declaring num you're declaring a variable second time you're just using it so you don't have to declare variable multiple times if you do that it will give you some bad words so i will say let num equal to 9 and if you run this code okay and that's bad words so you can see you can say it says identify num has already been declared and this is very important okay if you're starting your programming journey you will be facing a lot of errors as i mentioned before that should be a part of your life write a code get others solve them which is very important and the first step to solve a error is to read the error properly i have seen people okay so in fact when i started coding i used to write a code i used to get errors and the moment you get editor directly jump to the code see what what is happening don't jump towards code jump to the error message and see what is what's going wrong so when you read the error it will it will specifically mention identifier num has already been declared okay so that means you have you're declaring multiple times and that's what we're doing here very important now let's talk about how can you name your variable as i mentioned before your name should be logical okay don't use a b and c i might be using it somewhere because i'm just teaching you but then when you write up code for the project or for assignment never use a variable name which is not defining itself example when i say num it means something when i say username it means something okay but then are there any rules example i can still use a right but other rules that this is how your variable name should be let's say this is username what if i put a dot here not allowed you can see we got an error there in fact not just here if i run this code uh it will give you error data it says unexpected token so dot is not allowed here it has a special meaning which will talk about that later so dot is not allowed what about round bracket not loud so what is allowed a variable name can have characters what is there numbers as well example i will say username one we can do that okay numbers are allowed and you can put numbers anywhere not the first value the first letter should not be a number okay that will not work in fact you can also use some special symbol one of them is underscore okay this this is something you can do and there's one more you can also use dollar symbol so you can use dollar symbol and underscore those are the only special symbols allowed other than that you cannot use any other special symbol for the variable name and numbers are allowed but not as a first character there are certain status you can follow to name your variable so whenever you have variable which has two words example if you look at this username here you can see there are two words right user and name and what happens is the moment you start reading something when you when you read a code it should be readable write it now if you have these two words there are two ways you can differentiate between two these two words because in english we use spaces right and spaces are not allowed here so either you can use underscore this is called a snake casing rule uh so you can use cases with the help of underscore otherwise you can use camera guessing rule so you can say user name where the second word first letter is capital okay so it it makes your code more readable and that's very important okay so in this series we'll not just talk about how to do something we'll also see how do you write clean code what are the best practices and those those things are important so that's how you create a variable so just to reiterate what is camel casing is whenever you declare variable name and when you have multiple words in one name doesn't matter is it for variable or something which we'll see later you will be converting all the new words first letter with capital case so in the earlier video we have talked about variables right and then we wanted to look at data types but there's something important we have to look and that is constants see what i will do in this code is we have a variable right now we know how to get a variable and the idea behind the variable is to store your data it can be in a format of numbers text or anything you want to store and then the important thing is the value of a variable can change and that's why we say it's a variable right so what i will do is i will just change this code for let's say finding an area of a circle right so let's say we have radius as our first variable of course radius will have let's say the value of radius is 5. i want one more variable what's the area of a circle formula it's pi r square right we also need pi so i will go for a short of my number which is 3.14 and then i also want to create a variable which is area now as of now i'm not saving in any value inside the area okay we will see that later radius has a value which is 5 pi has a value which is 3.4 area is empty as of now so basically we will be finding the area of a circle so for that we'll say area is equal to and you tell me the formula it's pi okay that's simple then you have to multiply this with radius squared how do you find a square of a number here maybe you can if you do a multiplication among themselves that's a square right okay so pi r squared will this work let's try so i will just go back and this is the magic of programming right you can save data you can perform operations and you can print it so i will say console.log and we'll print area that's it simple stuff right let me just run this code here itself so i will say run oh we got an error oh this is not def oh my bad my bad this is what happens when you're more focused on the output so the variable name is actually radius my bad sometimes it's good to get another zoid so let me just run this code now and yes we got the output so you can see we got 78.5 see we are not actually concerned about the output right we are concerned about the code here the output is right you can verify that on your calculator but what's important is we are trying to find the area of a circle now this radius might change in future example let's say initial value of radius was five and before doing the calculation i thought hey i just want to increase the radius size to six can we do this of course right that's the idea behind variable we can change the value of a variable true how about pi value what if before the operation i thought hey the pi value should be 3.22 will this work and if i run this code there is no error at least your computer is not giving you any error because of that 3.22 but the thing is the value of pi never changes it's a constant right the value of pi is a constant that means if you try to change this it should not allow it so in your code when you know that something is constant which will not change instead of using let we will be using const and we are saying hey the value of pi is constant no one can change it so if you run this code you can see we got the same output right we are not changing by any anywhere but yeah if you're thinking it will automatically give the value of 3.14 no that's not the case once you initialize the value then you cannot change it and now if i go back and if i try to change the value of pi to 5 and if i try to run this code it will give you bad words it will say assigning to a constant variable and this is important when you want to debug as i mentioned before always look for the others and read them so it says these are error on line number seven okay so you can see there's also a line number here which is seven it mentions what's the problem and then the text as well as a beginner in programming this is something you have to observe you have to learn how to read errors and after some time you know after a few videos we'll also see how do you solve errors if you don't know how to solve them we will use google and we will go to stack of one of the best website available to solve bugs okay so that's how you create a constant you mention a const keyword and sometimes you refer it as a constant variable it's an oxymoron because we have two opposite words so yeah but something we do we do that so this is a variable a normal variable this is a constant variable in short we say constant so we have talked about variables we have talked about constants so in the earlier videos we have talked about how to write a javascript code we have seen how to write variables how to work with constants it's time to understand one of the most important concepts in a language which is data types now what is important as i mentioned before the most important thing in any software is data right because most of the application we built we built it for the data to store it to process it to make something meaningful out of it maybe if you want to create a graph if you want to make a game as well it's all about data right now when you're talking about data it's also important to understand the type of it now what i mean by type if you remember in the earlier videos we have talked about we can add two numbers right and then we can do those things we can add them we can find something specific about the data based on the type example let me take a variable here and we'll name that variable as data and let's say the value of data is eight okay now this is the variable right we know about it and this is our value now this value will have a type now if you are coming from other language where you define variables and you also mention the type of it here in this case we assign the type to a value now this value here is of type number right okay so that means we can have a number type what about let's say i want to store my name here so in this case i can select user equal to i can even have my name here now this thing is a string so let's say if you talk about text we can call it as string type so this is what we call as types so this data here represents a type now in javascript basically you can divide this type into two categories so you can divide data types into two parts first is primitive and second is object we will not be talking about everything in this video there is something which we'll see later of course because we have not seen the advanced part yet and if i talk about those things now you might get confused so we'll go step by step so basically the type is divided into two parts one is primitive and one is object so in primitive we have number string boolean null undefined and symbol now simply something which came recently so we'll not be talking about much about that in this video we'll talk about the numbers we'll talk about string boolean null and undefined now whatever is not primitive so example if you have a value which is not a number which is not a string which is not a null or undefined it's an object okay so it's a very complex topic to understand the object part complex at this point once you go ahead once you understand the basics it's very easy trust me you know i always follow this line said by someone very famous i don't recall but the statement is nothing is difficult it's just unfamiliar so you just have to get familiar with the language and it's very easy okay so let's start with numbers now numbers are numbers right so what whenever you have a number let's say 8 5 15 any big number doesn't matter it's a number uh so in this case i will go for a bigger number now so i will say num1 equal to a very big number let's say this is a this is a very big number now with this big number you can perform any operation and that's the logic behind numbers right we calculate we perform operations maybe you want to multiply subtract you want to find the percentage whatever operation you want to achieve you can achieve that with the help of a number type so what operation i'm talking about so i will simply say num1 n2 so whatever variable you have i can perform operation unless i want to multiply that number with 25. now the only problem is how do you verify if this is correct okay let's try this let's see what happens so i will go to my terminal and here i will say first of all i will say cls so that it will get empty and i will run this so i will say node first dot js and when you say enter you can see we got the output the only problem is difficult to verify but then it says 25 into 25 it's 125 at the end we can trust that number so you can use calculator as well but this this works but then how long this number can be there should be a range right now on this print this is a range with a number you can have any big number of this range right and mind you it's a power of 10 so this is a length you can work with but the only problem is if you have a bigger number very big number it might have some inconsistency with the numbers so the safe integer range or save number range is this if you have your number between these numbers then you will not be having any issues of data consistency or number consistencies but then you will be thinking hey what about if you have a number bigger than that in that case we have a very special sub type of number which is called big int big int was not there in javascript from earlier stage it came later and that's why in most of the project you still work with normal numbers but yeah we'll talk about begin as well okay so you got this number right and we are able to work with it but what if you have a decimal point here so what if i say a big number dot 8 will this work now this type of numbers in number system is called floating point representation so when you have a decimal number we say floating point so let's say seven point eight just to make it simple and if i go back here and if you're on this call you can see we got seven point eight now is it a normal number yes so this is a number so by default all the numbers are represented in floating point so even if user seven it's seven point zero you can imagine so 7.8 also works there's one more operator which we're going to talk about so what if you want to know the type of uh value so example when i said this is a number this is a string what if i want to know the type in the output so there's a special operator called type off so let's use type of here so we'll say type of it's an operator and you just have to pass a number so mention the operator name and the variable name so type off the variable name num1 and let's see what happens so if you underscore you can see it says number the same thing you can do with user now if you look at user here it's a string right and now i hope i have saved it so if you underscore now you can see we got string same thing can be done with any other type which you have even floating point representation is a number now in this number you can also store some other type of data i'm not sure if you know about this there are different base number system we have base 10 so whatever number we use is a base 10 number system we can also go for binary we can go for octal number system or we can even go for hexadecimal so let me show you hexadecimal how that works so let's say i have number now where you can find hexadecimal so if you have done work on html css we specify the color code right and we specify that with the help of hexadecimal ffff or maybe zero zero zero zero or zero a o f so we do that right example here i want to represent the number with the help of hexadecimal the way you can represent that is by specifying zero first so say say zero and say x now whatever you mention after this will be a hexadecimal number example i want to say f so f is actually 15 the last number in hexadecimal and let me just print the number as it is so i will print num1 and let's see what the output you are getting so we got 15 this is working what if you say ff now this is 255 so you can see the calculation so this is 255. in fact there's one more thing with the number system you can actually represent floating point with the help of a exponential example when i say 1.5 e let's say 12. now this is actually exponential you can also represent this and you can print and you can see we got the value so this is the exponential so it is 1.5 into 10 raised to 12 and this is the output you got simple we can actually have one more fun here you know i have this problem i don't know if you have this issue so whenever i type amount so let's say if i want to transfer 1000 rupees so 1000 is easy because we have only three zeros what if you want to transfer one lakh rupees in that case you mentioned five zeros right and you verify it ten times do i have exactly have five zeros or what if you have a very big number so let's say you have seven zeros or eight zeros i'm not even sure how many zeros we have on the screen now but do one thing go pause the video and comment how many zeros you can see i don't think only i have that issue and that's why we got a new feature not a new feature but you can put underscores in between numbers so that you can differentiate it and easy to read right so now it says one two three four okay seven there's seven numbers seven zeros there only underscore yes you can't use comma there okay the way you do it in real world you can't use comma because comma has a special meaning in javascript okay this is working the other thing you can also do is you can work with infinity right it's a type of number again see number is actually very big topic so we are focusing more on that so let's say 5 divided by and we all know whenever you divide a number by zero what you get is infinity but do you really get infinity or do you get some weird answer in javascript let's see so if you run this code we literally got a type which is infinity so infinity is a type of number we're going to subtype okay what do you think if i divide a negative number by zero what do you think what will happen so basically we have a range of positive infinity and negative infinity so you can see if you divide negative number by zero it will give you negative infinity i mean not just this you could actually have a maximum value in the number system right so i can say number so we have a special interface called number we'll talk about that in detail later what this number means but imagine this represents a number so when you say number dot max value so this is a maximum value you have which you can use and let's print this and let's see what happens so when you run this code this is a big number remember this is something which we have seen earlier bigger range now if you have a bigger number and if you say plus 10 let's see what happens plus 10 should work i guess let's say multiply by 10. so the maximum value multiply by 10 and if you underscore you can see we got infinity so whenever you go beyond a maximum value you get infinity same thing can be done for the negative infinity if you want to go with maybe you can try min value and divided by 10 so that should work okay enough for fun with your numbers there is one more one last what if you have a very big number you want to represent example here i have a very big number i'm not sure if this is a really big number which will not be supported by javascript i don't even know how many five zeros i'm typing there i just want to print no one and sometimes you'll learn by doing stuff right so let's do it let's try and oh it's working so it does supports this big number anyway so you can try big in the bigger numbers here what's important is if you have a bigger integer and if you don't want to lose any precision example i guess i'm losing position here if i say one at the end i run can you see that we are actually losing data it says a number and then 10 raised to 24. we don't want that what we want is we want one as well so you can also represent this number with the help of big n so just give a n at the end and this will represent it as it is so this is big n a special type where you can have a bigger number so just say n at the end the only caveat here is if you add this number by any other number let's say 2 what do you think what will happen this will give you an error why is because it says okay as i mentioned before it's very important to understand the errors right so it says cannot makes big int and other types oh this is big integer now 1 and this is a normal number so in this case if you want to perform the operation you can simply say 2n again we'll talk about how do you convert this maybe you can convert this 2 into a big integer or maybe you can convert this number into normal number so this option of type conversion we will talk about that in a separate video okay as of now this is working so you can see we got 53 at the end so that's how you can work with big inc okay this is working properly now i know there are some curious minds they want to know what is happening behind the scene how everything is working behind the scenes we'll talk about that and separate videos i mentioned before we are here to learn javascript from start to end so somewhere in between once you know the basics we'll go with advanced part where we'll know how things are happening behind the scene very important right so basically we have talked about the data type so we have two categories we have primitive and we have object out of which in primitive we have talked only about numbers now in the earlier video we have talked about data types right and then the entire video we have spent on numbers this time let's go with the other premier types so let's talk about string first now what is string so whenever you want to store a text we will use string right example if you want to store your name so what i will do here is i will say let and i just want to store a message now let's say what message maybe i will go with my name i will say naveenready that's it i know that's not a message but or maybe you can go with a username that should work and then at the end here i will print the user now this is called a string this part okay so you are storing a data in a variable so the type of the data here is a string or you can say string literal because you're literally representing a string there i will just go back here and run so you can see we got 1920 as it is now of course you can perform some operations on it maybe you can get the value you can get one character but again we'll talk about those in a separate video how can you fetch one character of a string let's say i want to fetch this v is it possible so we'll see that later as of now this is how you represent a string but can we mix two string maybe let's say i have a last name and a first name so let's say i will say first name is equal to naveen and let's say my last name here is ready now in this case i can simply create a user by mixing these two words let's try so i will say first name oh how do i merge my two names maybe i can use first name last name that's what you do in real world right you give a space but since we want to merge them we want to concatenate these two strings i can use up less operator i know you might be thinking hey plus is used for addition right that's the issue here the plus operator is actually very smart it checks what is surrounding it is it numbers or is it string if it is string i will concatenate them if there are numbers i will add them smart right so i will just go back here and let's see what happens and if i run this code oh we got the output okay but the only problem is we don't have a space in between okay this is something we can solve this issue with the help of a plus sign so what we can do is we can have two plus so we can have first name concatenate with a space then concatenate with a last name and now let's try this so we got nowhere ready with a space in between so we'll perform some more operational string later as of now we just want to understand how string works right in fact here you know i just want one more thing we can actually perform some operations so let's let me just go back to the earlier username so here we can use some escape characters now why do we need them so let's say i also want to add a telescope at the end okay so naveen reddy the disco now i want to have the disco in double quotes so can we do that i guess we have discussed that before if you want to use double quotes in your text and because i want double quotes in the output as well in this case you can use a single quote at the start so you can use a single quote here and at the end as well you can use a single quote so that double quotes will have an impact in the output let's try and you can see we got naveen ready the disco okay but the problem is what if i have double quotes here at the start because maybe i also have a single quote in between so in that case you can't use any of it right so if you want to use double quotes and even if you have double quotes at the start and the end you can actually cancel the special meaning of these other codes because the double quotes single quotes they have a special meaning we can cancel that with the help of a slash so just give a backslash and done so whichever double quote you want as an output just say backslash and it should work let's try it so yeah it is working so you just have to say backslash and it will cancel the special meaning of it now this is called a escape character now this is not the only one we have we have so many characters available example let's i want to print ready on a new line so i want to print naveen and then i want to print ready on the new line so let me just remove the subject for time being so that's job is done so i can simply say slash n now slash has a special meaning which is of new line so if i run this code you can see we got new line we can also use slash t for the tab so let's say i want to give a tab after no so n a tab and then win so in this case i can say slash t which is for tab and you can see we got no then space then what if you want to have a vertical thing so i will say flash v which is which will give you a vertical tab so on the new line basically let's try oh it's not working here is it saved this is not working let me just try with some other character here okay so this thing is not working on windows terminal so try this on your machine this is actually a vertical tab i'm not sure why it is not giving us a vertical tab so let me just use that here okay it's not still not working so try this on your machine if you are using mac or linux it does behave properly i'm not sure maybe it's a powershell issue try that with your bachelor or command shell or your terminal then we also have let's say i type extra d here you can give a slash b to delete that extra character so let me run this code and you can see we got only one d okay because it will remove it will give a backspace when you say slash b okay this is how string works the next thing we can look at is boolean now boolean is either true or false example your computer can make proper decisions because of true and false right so let's say uh we have a boolean type let's say bull equal to so let's say i want to perform some operation here i just want to check if 5 is greater than 6 is it true of course not this is false right so how do we get the output what we can do is we can print bull and let's enter and you can say we got false uh you can also try with less than symbol and you can say we got true so this is how you perform the condition and it will only result in either true or false that's a boolean values in fact you can also check the type of it so you can say type of full and it should print boolean that's right we got boolean type right so we have seen numbers we have seen string and we have seen boolean again we'll talk more about how do we compare stuff how do you perform operations maybe you want to do some other operations maybe you want to compare two numbers to strings we'll say that later or maybe a string a number okay as of now we just want to focus on true or false so apart from this we have null and undefined right now by chance if you're coming from some other programming language let's say java c c plus plus we have a concept of null there right which simply means it does not exist right example let's say if i have a name here or maybe a user and if i say user equal to null we can see it's a keyword basically so none is a type so basically you know when you talk about null it represents something called empty or does not exist but if you print user see logically it should print null right and it is printing null we don't have any value there but if you print the type of it we got object and that's weird okay you can call it as a bug or a feature which no one want to remove but yeah we got null so if you want to convey that we don't have the object or we don't have a value you can signal but then to do that especially in javascript we have something called undefined example if i don't define here so if i select user and let me only print user here so what it will do is it will print undefined and this makes much more sense right the variable is not defined and the amazing part is the type is also so if you print a type of undefined it is undefined so this makes much more sense to use that depends which one you want to go for mostly we will be using undefined but yeah if you have an object and if you say don't use this object anymore or clean this object you can use null there but the only thing is null represents an object it says it is not an object it doesn't represent an object but it's an object okay so once we start with comparing we'll understand true difference between null and undefined but as of now we got this there's one more thing i forgot to mention about numbers what if i have a num here and let's say 5 i want to say console.log and i want to perform some operation five so i'll say five divided by r string let's say naveen what would happen so if i run this code you can see we got n a n which is not a number so nan is not a number but and then type itself for example if i print type of this operation so i want to know what this operation returns if i run this code we can say you got number this is actually not a number but the type is number this is something which is confusing but understand this the output is nan which is not a number but the type is a number okay so this is something you have to remember mostly for the interview purpose but for programming we don't actually focus much on this it's not a number you solve that and it should resolve now this video is important but not just that it's also interesting and the topic is the history of javascript see every language doesn't matter is it cc plus java php ruby every language has a history some histories are interesting some are not but javascript has a very interesting history and the history is important because whatever you do whenever you work on a new feature you should know in which version it came and will it work on the browser which you are working with that's very important right so let's talk about the history now to understand that let's go back to 1990 of course that's way back right that's even before javascript was launched now in 1990 one thing got famous which is the internet of course no one was knowing about it everyone just heard about the concept like internet the way you know about blockchain now you're just not the word right you don't know the technology in the same way internet was new not everyone knew about it and even if you ask someone what is internet they will be they will be saying hey somebody will connecting multiple machines or you're sharing information and that's the idea right internet was all about sharing information but who will use it and that's what we thought hey you know we have a lot of companies and they want to share their information instead of using templates the way you use those yellow papers on the public places you know you distribute the templates in the same way you can share your company information online and anyone can see it so if you have a computer just go to that website and you can see the information and that's why everyone wanted to have their own website provided they have enough money right so let's say if i'm the company i want to have one website i will ask someone to make a website for me and that someone will be called as a html author i know that sounds weird but that's what they've used to call them the html authors because they used to create web pages they should design pages with the help of content and to do that we just need two things html css designer page you're done and those pages were static pages every time someone goes there they can see a simple page yes there were hyperlinks you can jump to one page and from the other page but then it was it was not interactive and so in 1993 when they wanted to have a good browser of course right to view our web page you need a good browser so we got nozia so moziac was one of the best browsers available at that time so it got famous right and everyone wanted to have their own browsers now so most of the tech companies they thought hey that's a good business let's have our own browser and that's where the founders or the developers of mozilla thought hey why don't we create a company and we'll commercialize this and then they founded a company called as netscape now netscape created their own browser called netscape navigator now it was awesome right but since we got netscape navigator there are other companies as well who thought hey you know we got a new company who are able to get their own browser we are tech giants can we have our own browser i'm talking about microsoft so microsoft thought hey we have a lot of money we have a lot of developers they can build a browser for us and that is internet explorer but the problem was in in the case of netscape you have to buy netscape navigator there was a time where people used to buy browsers right but it was it was worth right when you are able to access the internet that means you are rich that means you have money at that time right so people who are buying netscape navigator the entire business model of netscape was based on the browser they were making money because of netscape navigator but microsoft came into picture and say hey you don't have to pay for internet explorer in the windows os they added a component and then you got a free browser right and netscape here thought hey we need to do something we need to do something so that for for two reasons one they want to stay in the market and second they want to give awesome experience to the users they i mean no one wants to see a blank page or a page with just content no interactivity and that's why they thought can we create some components which will be displayed on the page so they wanted to have that and they they approached sun microsystems now at that time in 1993 of four java was getting very famous it was not publicly launched but then it was getting used in the enterprise and they thought can we use this language on the browser maybe for the interactivity let's say someone wants to give give some information if they want to see some animations on the on the web page can we do that because java was having applets can we use applet on the screen that was the idea but then it didn't work that way they wanted to go for sun microsystems java but they thought can we create a new language and that's where they hired brendan h brenden each was a developer who was working on scheme language so he knew scheme so he thought can we just build a new language so netscape said hey brendan you have to make a new language using which user will be able to interact with the website just make a language which we use using which you can make beautiful pages not just the way they look but the way they interact and the deadline given as soon as possible maybe if they might have said today itself they want a new language but brendan took 10 days to make a language maybe this is the first language which was developed in 10 days right so we got a new language in 10 days and of course right whenever you have something that quick imagine you're building a language or programming language in 10 days it will have some bugs it will have some issues it will have some awesome features the only caveat is the language which was developed then is very different from what you're using now it was a small language with less features now you have so many features available right but still it has bugs and the same bugs are still there in the language because the language is backward compatible so enough about the issues let's talk about the language and the name of the language was mocha awesome name right so initially they went with mocha name but while launching they changed the name to live skirt so that was a good name but the only problem is in the internet market it was new not everyone wanted to have their own website so can we just market this language as an awesome language and of course netscape wanted to market it in such a way that this language will be used only in the netscape navigator and if you want to access the pages you have to buy a netscape navigator that was a strategy they renamed the language from livescript to javascript because java was famous right and then can we use the same name so maybe they wanted to show it's a cousin of java technically they have some similarities because netscape approached sun for the language they copied some of the concepts from the c family right c c plus plus java so it has curly brackets so they thought hey that that makes sense right let's have a name as javascript so somewhere in 1995 they released their own language called javascript in the new browser called netscape navigator 2 and everything was good right so somewhere netscape was actually winning the competition right because all the other companies don't have a language now in this case microsoft says okay you got your language so now that's one zero you are winning but don't worry what net what microsoft did is in fact they have a lot of resources they have people they have money so there's reverse engineered javascript and they created their own language okay now this is a tricky part first of all they reverse engineered the language javascript so of course the new language will be similar to javascript now the second question was what should be the name of a new language they did some research they worked very hard they thought about this for two minutes and they thought hey let's go with a new language called jscrip so that's how we got a new language called jskip so now we have two languages javascript and jscript the thing is javascript only works on skip navigator jscript only works on ie now as a developer when you are making a website or as a designer when you are making a website which one you will choose so if you are making a website only using javascript it will only work on netscape navigator if you are making a website using jscript it will only work on ie and that's why in fact that was a big diversion and that's why most of the websites they were mentioning at the bottom best viewed in ie or best viewed in netscape navigator so that was a big issue right so first of all they copied the language they copied the name plus they made internet explorer free now when you get the same feature of a browser so let's say if i give you two applications when you have to buy it one is free both are same of course you will go for the free version right and everyone went for ie for two reasons it was free and everyone was using windows that was the only major os available right so windows was having the maximum market share i was getting a lot of market share and at this point let's keep thought okay now that's tricky ie is again winning the competition microsoft is doing the competition now so that's one one or that's actually two one because microsoft has market share it has a free ie now in this case netscape went to ecma basically it's a ekma international which stands for european computer manufacturers association so they are responsible to create standards so basically we have two languages doing the same thing and they are not they don't have anything in common so can we create a standard so that javascript will become a standard language which will be used by everyone and that's what happened so ekma used javascript they created a new standard called ecmascript now ecmascript is a standard with definitions and this the protocols so whenever you have a new language it should follow that ecmascript the advantage would be in future if you are using a language irrespective of javascript or some other language but if that is following ecmascript that means you have something in common you don't have to write best viewed in ie anymore so that was the idea the amazing part is ecma is not just giving standards to javascript they have multiple technology to give standards to like c sharp and c plus plus cli jason so the first part of ecmascript which is ecmascript 1 was launched in 1997 so somewhere everyone thought now javascript is a main language right it's a primary language so people were moving towards javascript everything was going good but then in three years of course in three years we got three ecmascript and somewhere microsoft thought that's the future right we have to follow ecmascript but then they had two mindset here first they don't actually wanted to go for ecmascript because it's a representation of javascript but then they were not having any choice right so there was a confusion here but in 1999 something happened in fact in 2000 something happened the dot-com bubble burst because at that point as i mentioned before everyone wanted to have their own website rights and then if you have it on website you need someone to build it so as a company if you are responsible to build websites you will be getting with a good amount of investors and the value of your company was high very high it was a bubble actually the way the things are going for dodge coin right so it was a bubble and which got burst in year 2000 the entire tech market was going down not all companies some companies survived and they are the leaders now the company who survived the dot com bubble the the moment you have a dot com bubble everyone lost hope from the tech world at least from the internet world they thought it's not going anywhere but internet is still there websites are still there but it took some time to you know to come into that picture again until that point in in fact in 1999 netscape was so much in loss so netscape got sold and the company who bought netscape was aol so that was the end of netscape but before selling the company what they did they took the source code of netscape navigator and they made it they made it open source any guess the the reincarnation of nescam navigator firefox so the firefox browser which you use now is actually the reincarnation of netscape navigator now coming back to ecmascript what happened after 1999 because we got ecmascript 3 what about the next version so we got three versions in three years the next version was on waiting because the next version was ecmascript ford the problem is they in that two years or three years gap they thought we can include a lot of new features looking at other languages we have so many features right we don't have them in javascript can we just use those features this is good this is good this is good and they have their own ideas but the problem is whenever you have this bunch of concepts coming together it's difficult to implement those things in one language in the next version and plus they were not having enough motivation to do that so it was getting delayed and delayed and delayed because no one was able to implement the concept properly and then and it was not able to release so they had those concepts and they kept it aside and then they launched ecmascript 3.1 which was which was having some minor updates but not anything not anything major what happened to ecmascript 4 it was not implemented in javascript but then there was a company called micromedia now micromedia said okay this is our awesome specification let's have our own uh scripting language which is actionscript so micromedia went with accent action script right which is which is ecmascript 4. so things were not going anywhere for javascript it was still called as a client-side language and it was there but nothing major was happening something happened in 2005. in 2005 we got a jax so someone wrote an article someone wrote a paper by saying hey now we can actually change the data in the page itself so example in those days if you want to change the date of a page you have to go to the next page so even if you want to do a small change in a page you have to call the next page but nowadays we have a concept of single page application and that is possible with the help of ajax so it was very famous right so ajax is something which has given the boost javascript needed so again in 2008 when we got some new features in javascript which was it was getting used ajax was getting famous jquery was getting famous someone thought hey can we use these features because see to run a javascript language you need a engine because javascript being a interpreted language that's right it's an interpreted language so what is interpreted language means so whenever you have a source code it doesn't matter which language it is if it is an interpreted language the execution will be done line by line so let's say we have 10 lines of code it will execute line by line the only problem is it is slow because at the time of execution you are passing the lines on the other hand when you say compiled languages the the entire code get compiled right you find errors if there's any others or not if it is getting successfully compiled that means there is no syntactical errors you just have to create a file and that file will be executed on the machine which is bit faster and javascript was slow because it was interpreted something happened in 2008 google came up with their own browser and every browser had their own engine example in ie we had we had a different engine for modular firefox with a different engine chrome says hey we will give you a different engine a v8 engine what's so amazing about it the amazing thing is now your javascript files will be both compiled and interpreted and that's amazing right it introduced something called just in time compiler and that was amazing so now since it is fast can we use javascript on a machine so now it's a easy language and it is fast so they wanted to write javascript application which will run directly on the os and that's where we got a new environment called node.js so it's a node.js node.js is an engine or it's an environment which has a javascript engine so basically using node.js you can build application not just as a browser front end but the backend application the desktop application anything you wish which you want and that's where javascript got the boost which you you can see now and in the same year you've got ecmascript five so in 2009 we got ecmascript 5. can you see that the gap of 10 years in 1999 we got ecmascript 3 4 was crap so we got five in 2009 that's amazing and you can imagine how many features we got and some someone called it as a modern javascript because it has so many features amazing features and then it was getting famous because you can write applications on the desktop you can write application for the websites for the backend servers everything with the help of node.js and then they thought okay now we got some new features in 2015 we got ecmascript 6. now that's a new benchmark so every time you have amazing features you say that's a new benchmark right ecmascript 5 was a benchmark ecmascript 6 was our next benchmark now after that in 2015 as well we got so many new ecmascript versions but still we talk about ekma66 because it introduced new concepts which is getting used by everyone now the current version of ecommerce script is ecmascript 12. now can you imagine that in six years we got six new versions it's because after es7 they decided to go for yearly versions so every year you will get new version so we got es7 in 2016 we got es 8 in 2017 now instead of calling as es7 or the s8 they changed their nomenclature so they said okay enough of es5 es6 let's go for the year number now because the updates will be coming every year so officially we can call it as es 2015 es 2016 years 2017. so the current version is not es12 we will not call that we'll say it is 2021 right so it is coming in june i guess uh so that's the latest version which you can use now the question allows if you want to learn javascript which version you will be using you will be using 6 or 10 or 11 or 12 that depends upon the browser compatibility the moment you use new features it will not work on the old browser so the language is backward compatible that means you can still use all features but it's not forward compatible so you can't use new features in the old browsers and that's why we need trans trans compiling where you need a certain libraries which will help you to do that now what happened to jscript what happened to actionscript everything got scrapped so no one is using jscrip now no one using using actionscript the only language is javascript so what happened to macromedia adobe acquired micromedia what happened to sanneco system oracle acquired some ecosystems who owns the trademark of javascript it's oracle why is because remember sun was involved when they wanted to have a language javascript so the trademark was registered by sun or like albot watson sonar treadmill belongs to old michael you can still use it they're not claiming any trademark there but it's worth mentioning now in this video we'll talk about type conversion and type coercion now why do we have two different words let's understand that now first of all it doesn't matter which language you work with you have something in common right you actually work with data but sometimes you want to convert your data in specific format example we have talked about data types right we have numbers string boolean and there might be an option or there might be a requirement where you have to convert your data from number to string or from string to number or from number to boolean how do we do that now let's say let me create a variable here so i will say num this time i'm using a dark theme basically a high contrast so let me know your feedback in the comments section uh so finally we are on the dark theme yeah let me create a number here right so this is a number now we have seen how do you print the type of data of course when you want to print the data as it is we can simply print num right now this will print data example if i run this code you got 6 but what if you want to print a type of it in that case you can use a very special operator which we have seen earlier which is type off and you can print the type of num so basically we are printing two things here we are printing num and we are printing type of num and let's see what happens and you can see we got six and we got number that means the type of this num is number that is working right but what if i want to store this number in a different format i want this to be in string format can we do this now you will think hey why do we do that so example let's say if you want to store a pin called a zip code of area which is the best format should you store the zip code in number or in a string format see whenever you don't want to perform any operation it's better to store that in a string format example telephone number a zip code or whatever big number you have when you want to perform calculation that's why number makes sense right so here i want to store this six in a string format in that case you have to convert that right you have to perform some operation so we have a specific thing called a string so we'll pass string here when you pass this number 6 inside a string it will give you a string format so this number is getting stored in a string format now will it work let's try so let me just run this code once again and you can see we got 6 no change in data of course you have not got s i x what you got is number six and you've got a string okay so this is working right and you can also do reverse if you have a number in a string format let's say one two three so now if you run this code of course the data is in string format one 123 is in string format and if you want to perform operation you have to convert that into number in order to do that we can simply say number and you can give a bracket and this will work right so if you run this code you got number the same thing you can do with boolean okay so you can convert a number up with boolean but that's a different thing we'll do that later the interesting thing is this is type conversion right you are able to convert a number in different format this is explicit conversion because basically you as a developer you are trying to convert a data from one format to the format it's working right but then javascript went ahead javascript says hey you have type conversions that's great let me give you something called type conversion now what it means so let's say i have a number here i'll say num or maybe i will not go for any specific name because num represents a name right so i will go with let's say x i know it's not a good variable name but just for the example here we can go with it so let's say i have a variable x and i want to print x here and i want to print the type of it of course we are printing the data we are printing the type of this data and the output you are getting is undefined undefined of course that makes sense right now what i will do here is i will assign data to it so when you assign the data it will change the type of it now what did i want to assign let me just do that here so i will say x is equal to six or maybe a different number this time so let's go for x equal to eight now if we do the same thing i just want to copy and paste actually i'm just reusing the code right so after changing the value of x now let's try so you can see we got undefined undefined because that was the first time and then we got eight number because this is the number if i change the same thing again let's say i want to perform some operation so i can simply say x is equal to i want to add x with a string what do you think what will happen and what type of string i want to add maybe i just want to add empty string i don't even have some specific type of string here right it's it's an empty string now what will happen of course i should also print the console here okay now let me try and you can see this time we got string so basically whenever you have a number and you are you have a string this plus operator here it got confused the idea behind a plus operator is to add two numbers right but this time we have a number and a string confusion right so coercion comes into picture so javascript says okay in this case every time you have two different data and you want to perform some operation you have to come to some consensus right either both has to be same type it either it should be both string or both number so in this case it goes for the string because we have string at one end now whenever you have string and when you use plus operator it is actually concatenation so basically we are joining two strings for example let's say you have naveen you have red d two different strings you use a plus orbital basically you are concatenating it in this case as well even if you have x which is a number and you have a string it will convert that into string but not always not always when you use a string a number it will give the same results example let's say at this point if you see this is a type of string right this x is a type of string what if i do x equal to x minus 2. now you tell me what will happen of course i have to print it as well so pause this video and let me know what will happen because x is a string now 2 is a number so how it should behave should we get 8 minus 2 6 or should we get some weird answer or maybe it should be nand right which is because you are subtracting a number from a string and that is weird so let's let's see what happens so if you run this code i hope you have answered that in the comment section you got number so this is what happens when you have a subtraction operator with a string it will convert that into number so this is type coercion so based on what type of operator you're using based on what's the requirement javascript engine will change that for you in fact we can also do with boolean so we know right boolean is true or false now the way you can manipulate boolean is with the help of some operators one of them is exclamation exclamation will simply replace or it will do the opposite so if it is true it will give you false if it is false it will give you true right so exclamation actually means not so there's a not operator with boolean which will change the type so from true to false false true now let me just do that so i will say x equal to not x so even if it is a number format so you can see x in this case is number right so you are applying a not operator or number because the naught will only work with boolean right and you are giving a number so in this case it will convert that into boolean and then it will perform the not operation and let's see what happens but the question is will you get true or false so even if i say it converts this from number to boolean what happens is it true or false let's try let's run this code oh i'm not printing it my bad let me print this and you can see we got false boolean so boolean makes sense but why false that means you are performing an operation the output of that operation is false that means initially the value of x was true that means if you try to convert a number into boolean if that number is six it will give you true only six it's actually any number example let me just do that so what i will do is i will just go to console and console.log and let me convert a number let's say i have a number seven and i want to convert this into boolean how do we convert a number into boolean the same way we did it for the number and string with the help of that keyword number string here let me use boolean so i'm passing seven to a boolean right so it will convert this seven into a boolean format but what you will get you will you get true or false let's try so when i run this code you can see we got true okay so for six it is giving you true for 7 is giving you true i thought it will be even odd so even numbers will get true or number will get false but that's not the case every number is giving you true let me try negative number so if i say minus 7 let's see it's still true okay now after trying out all the different combinations let's try zero and the moment you try zero and if you underscore you can see we got false that means all the numbers are true only zero is false why is because the actual implementation of boolean happens in that way zero is false one is true right if it is not zero everything is true you can actually calculate with each number so they simply go with simple formula if it is zero it's false if it is all the other numbers it is true and this is called a truthy and the falsie values i know that that's tricky it's not just with numbers it's also with other types example let me convert into boolean of type null now what will happen if you try to convert null into boolean and if you run this code you can see we got false so even null is a false value so whenever you try to convert something into boolean and if that gives you false it is false value if you pass something and if you're trying to convert that into boolean if it is giving you true that's called a truthy value example after some time will start working with functions objects whatever your data you pass so if you try to convert object into boolean it will give you true if you try to pass a function to and convert into boolean it will give you true cool what about undefined let me just try that so we'll try it with undefined also it will give you false so these are all false values okay apart from this everything is truthy value so you can see the list if you have something like this and if you try to convert these things into boolean you will get false and all the other thing will give you true let me just try with a stringer we have not tried with string yet so what i will do is i'll pass the string like naveen now what happens now it is true or false of course nothing is true let's try and you can see we got true now since it's not a part of the false values it's true so just reiterate whenever you have a number and a string it will convert that into string when you apply the additional operator but when you perform this subtraction operator it will convergent number so javascript is smart enough to know what should be done and that's one thing i love and hate about javascript we love this thing because it helps you it actually understands what you need and it will give the output the reason i also hate it is because it tries to convert things the way it wants sometimes you don't intend to do that and you want others but javascript says okay you're doing this i will allow you and i will help you here we don't want your help we want others because this will eventually lead to bugs okay and that's why you have to very alert when you're doing these operations in fact we have some more ways example let's say i want to do plus operator here right so we have x in string format here and you want to okay let me just remove this part just to make it simple yeah so we have x which is string format right so if you run this code so you can see we got 82 because you have 8 here which is x and then you got 2 so you got 82 but in this case i want to convert this into a number in that case you can use unary operators of course we'll talk about orbitals later in detail but so whenever you add a plus here it tries to convert this string into number let's try that and you can see we got numbers so we got 10 which is the addition of eight and two and you've got a number okay there are more ways actually to convert the string into integer or the other format so let me just try that so let's say we have x and the value of x is one two three naveen okay now in this case i want to convert this into a number format but of course you can't convert that into number format right because we have naveen there so if you try to do that with the help of a number function so let's say if i do let me just print that first as it is let's see what happens if you print this and you can see we got one two three naveen okay that works but if you try to convert that into number because we did that before let's see what happens so if we try to do that and if you run this code you can see we got nan which is not number we don't want this what we want is it should be able to convert that and at least fetch these first integer values so in this case you can actually use parseint so pass int is a special function which will accept a string it will try to convert that number okay it will do its hard work to convert a string into number and you can see we got 123. but what happens if you have a character at the start in this case it will not work so i have to make sure that you have numbers at the start so it will try to convert that into numbers right so that's how you can do it so try out different functions you can go to google and search for different type conversions functions and you will get those now in this video we'll talk about arithmetic operators now see when you talk about a language or you talk about surface you perform some operations right now it can be calculations it can be logical operations but here let's focus on calculations and we do that a lot on computers as well right so let's say if you want to add two numbers you want to subtract two numbers you want to find a division of a number you can do that and for it you need to use certain operators and the thing is in the earlier videos we have talked about most of them but still you know just to complete that topic let's talk about in fact once we reach to the end of the video we'll talk about some important operators and some tricky parts so let's get started so what we'll do is i will create a variable here num and usually the value of number will be let's say 1 and i want to perform some addition subtraction multiplication division just to show you that so what i will do here is i will take another number here which is result and with this number i will perform that operation or maybe just to make it more effective let's take some other numbers so that we can perform all the required operations let's say four and two and we know because what happens when you use these simple numbers easy to calculate at least in our mind so what i will do is i will say num1 plus num2 and we know it right this is addition operator so we'll print it and we'll print result okay this is the addition of two numbers and then we'll print let's run this code and let's see what happens so if you run this code you got six that is that is good and now i want to find these subtractions very simple so just we are trying to complete that as soon as possible because it's very simple in fact in the earlier video we have also talked about what happens when you have different types of data and when you apply these operators but as of now just to keep it simple we are going for numbers right we can actually also perform the division operator when you say divide you can say we got two in fact you can also divide the number by zero which will give you infinity or you can also divide a number by a string it will give you nan you can you can try those things right but then we know what is addition subtraction division can you multiply yes with the help of this hashtag symbol and if you underscore we got eight so basic operation is very easy right what about if you want to find a remainder so in this case we'll use a percentage operator so let's try and we should get 0 right so 4 divided by 2 and the remainder is 0. so we can try with other numbers as well let's say if you have a number as 5 and then if you find a remainder by doing that with 2 you will get 1. okay this is working so no issues with this thing right and then if you have two strings i mean string and a string it will give you concatenation right but what happens example let's say the num1 is not actually a number but it's a boolean value true and num2 is also true and the operation you want to perform here is addition now what will happen pause this video go to comment section and type the answer what do you think what is the answer here now logically it can be true because if you know the concept of complex logical operations so when you have two trues it might be something like that or it might be nan because you can't add boolean values let's try what happens i hope you have answered that in the comment section we got two why why we got two here that's weird see it's actually very simple remember when we talked about truthy value and falsely values whenever you have a true and if you try to convert that base because we are using a plus operator here right now type coercion it says okay when you have a plus operator and on both the side you have boolean values let me convert them for you so this number will be converted into numbers and the first number so zero is false so one is true and that's why we got two right so one plus one is two very simple okay this works now whatever what i want to do is let me just remove result for time being and let me also remove num2 let's only focus on num1 here and when you have only one num let's go with num and here let me print now okay let's go with number four again and let me run this code and you can see we got four let me just clear the screen now what i will do is i will just add this number by itself so what i'm gonna do is i will say num is equal to num plus two so what i'm doing is i'm taking that number four i'm adding two so this makes sense right so it should give you six because here what we are doing is we are taking the value of num which is 4 4 plus 2 is 6 then it will assign that's how it goes so first it will try to solve this expression okay this expression what is expression whenever you have something which will be getting resolved to a value with this expression example num plus 2 is expression 5 plus 2 is expression high is an expression 5 is the expression because i can assign 5 to a number so here we are saying num plus 2 is an expression which will be assigned to num which is 6 in this case and you got the answer now when you want to add a number by itself example we are adding num by two right so we can use a shorthand so instead of using this syntax let me recommend this here so we can use double slash to commit a code and then we can use shortcut so we can say we want to add a num itself right so we can say num plus so we want to add num itself with two okay this is this is called shorthand operators so instead of using this syntax we can use this so we are saying we want to add num by itself plus 2. let's try and it worked right it worked so we can also do that with the number one so let's say if you want to do this operation which is num equal to num plus 1 in this case you can say num plus equal to 1 so this should also work we should get 5. but specifically when you have only one you can actually use a special operator which is called num plus plus plus plus operator so it also means increment so if num is five it will give you six if num is six it will give you seven so num plus plus is an increment and that's why if you know there's a language called cnc plus plus c was the initial version c plus plus is a next increment of c right so that's why they went with c plus plus name oh we have not seen the output yet which still works right so instead of using num equal to 1 plus 1 we can say num plus plus okay so this is how you can increment what about if you want to do this num equal to num minus 1 any guess you guessed it right we can use num minus minus right and this works so plus plus will increment minus minus will decrement in fact the fun part is you can actually use plus plus at the start so you can either say num plus plus or you can say plus plus num both will give you the same answer in this case okay why i said in this case actually the thing is they both have different names this is called the syntax which we are using here is called pre-increment and when you say num plus plus this is called post increment in fact there should be a separate video talking about these things but let's try to do that here itself so we got num plus plus and we got plus plus num one is pre-increment and one is post increment what's the difference see in both this case example even if you try this syntax or this syntax it will ultimately give you the next iteration right uh so what i'm doing is i will run this code what is your guess what will be the output it should be six right because here we are saying plus plus num which will make this number four to five so this is five here and at this point we are saying num plus plus it will make it six so if i run this code we got c so this is working right but we still don't know the difference between pre-increment and post increment let me do that oh before even before going ahead what if you have minus minus num and num minus minus what it will be called pre-decrement post decrement right so name changes but just to understand the difference between this two let's go with this example here so what i will do now is let me create another variable which is x equal to and i will say x equal to num plus plus okay so now what we are doing is we are incrementing num that makes sense and then we are assigning the value to x so this is an expression which will give you a value and that value we are assigning to x and then let me print x here actually let me print both let me print x and then let me also print num and let's see what's the difference between these two numbers so what do you think even before running this code what is your guess and please also mention the timeline okay because the problem is in this video i have asked you for so many questions to answer that in the comments section so what you can do is for different answer you can give a timeline this at this point this is the answer so that it will be helpful for others to actually know what you're thinking so what sometimes what happens you know when when you are learning something new and then if everyone is getting the right answer you're getting the wrong answer you feel bad but what if everyone's getting the wrong answer and you're getting right answer you feel good what if everyone is getting wrong answer you feel way better because not everyone knows about it right so it's a good feeling okay so come back so here we are doing that and let me see what happens before that i will clear the screen and run and you can see we got four and five so we got here four oh that's weird why we got four see num five makes sense because we are incrementing it why we got 4 here it's because this num plus plus is called post increment now what this post increment means so post increment just means that it will first fetch the value then it will assign the value so basically we have two options here increment and assignment so we are assigning the value to x right and then we are also incrementing it which will happen first since this is post increment it will first assign the value then it will increment so before increment what's the value of num it's 4 so 4 will be assigned to x and then it will increment okay and that's why we got 4 for x and 5 for num what will happen when you do this now this time we are not doing post increment we are doing pre-increment now you should be able to guess it right it should be five and five now why fifa is because we are first incrementing it pre-increment right pre so it is first incrementing it making it the value of 5 and then assigning to x which is 5 in this case so that's the difference between pre-increment and post increment okay so before ending the video we have something more let's say i have this number 4 and i want to find a cube of this number how do you find a cube of number it's very simple let me say let result equal to you can say num into num into num so that's how you can find the cube of a number right the other way you can do is let me run this code first let me just print the value in the console and let's see what happens you can see we got 64. but what if i want to do something else so we got a math class math interface like of course we'll talk about class interface later but as of now we got something inbuilt so let's imagine we have inbuilt function called map that power and in this you can pass two parameters four comma three now what is four the actual value and then three is cube or if you want to find a square you will say two so this is actually a power so when you say 3 you are finding 4 raised to 3 which is 4 power 3 and that's why it's a power power right so if you go over it it will say it's a function or a method returns a value of a base expression x is the base value and y is the exponent value right so this you can use this and it will give the same answer right but then in new version which is es6 we got something new and that is using double star or double dash tricks so when you use single asterisk it is multiplication double asterisk means power and you can see we've got the same answer if you want to find the square you can do that if you want to find 4 raised to 4 you can do that so that's with the help of the asterisk symbol double asterisk symbol in this video we'll talk about relational operators now what are those so in the last video we have talked about arithmetic operators right where you have two operands and then you use a operator in between so let's say now we have two values and you want to compare them maybe you can use less than symbol greater than symbol you want to compare two numbers or maybe you want to compare two numbers if they are equal or not let's do that in this video now this video is interesting because you can compare different values right but there's a twist in this topic so this is not like all the other languages which you have gone through maybe if you have learned other language like cc plus plus java is a bit different in here so we'll see that at the end of this video so now let's start with the concept so let's say example let's say we have two variables here or maybe we can directly go for console.log and let me just print what exactly we are trying to compare here so let's say i want to compare two values here now when you say you are comparing what's the output so when you compare and when you get the output the output will be in a boolean format now this boolean can be true or false right so ultimately it comes down to that boolean value either you will get true or you will get false right okay so when i say 5 is greater than 6. now in this case what do you think will be the output now we are comparing two numbers and then we are saying 5 is greater than 6. now that's not true right so it should give you false so if you save this and if you run this code and let's see what the output is and if you enter this you can see we call it false of course right it's a false value okay so what i will do now is let me just increase this value so let me let me say 7 now let's compare and you can see we got true so that's how you can compare two numbers right but this time what i will do is see so now we know ultimately it's all about true and false right so what i will do let me create a variable and i will say this is bull so a boolean value uh and the operation which we are doing here or maybe i can say this is data just to keep it simple right uh so now i will say seven is greater than six and instead of printing the comparison here we are printing data so it will print either true or false okay we got true because we are not changing data here we just changed the way we process this so we have a variable now it is quite simple when it comes to numbers right so let's say if you have two variables and if you want to compare which is greater so you can do this now okay uh but let me just make it bit complex can i use lesson operator of course we can it will be giving you false of course right 7 is not less than 6. okay but what if i have two values which are same i want to compare if they are equal or less than so i want the first value to be less than or equal to the second value in this case you can use a combination of less than or equal to if remember in math we do that right we say less than on on top of that draw a line so the same thing we can do with less than equal to of course we don't have that symbol on the keyword right so we have to use this uh will this work let's try and it works we can see we got true now the best thing would be you can save this data in a variable let's do that quickly so let's say x is equal to six of course this is not the best way to write your variable names but since we are learning that should work and now instead of using this 6 and 6 will use x and y so that's how you can perform the operation you can you can compare two values the same thing can be done for greater than equal to uh so since they are equal so it should give you true right so you can compare this way so you can say less than greater than equal to okay can we do this for string now this is integers or this is numbers it makes sense right you can actually compare numbers what about if i have a value which is let's say pen and then the second one is book now can you technically compare two string and how the how will that work so let's say i want to compare if the first string is greater than the second string can you really do that and even if you compare them it is based on what so think about it okay so if you compare two string what will happen think about it pause the video comment your answer and then we'll see what is happening so let me run this code and let's see what happens so if you underscore you can see we got true that's tricky why we got true so it says the first value which is x is greater than y which is pen is greater than book might be but how it is comparing is it the philosophical way or it is just a comparison of values so what is it is doing so it is comparing each character from start like dictionary comparisons uh so it will start with the first letter so is it p okay it is b so p is greater than b in in terms of the alphabet number so what happens is every letter or every character which you use here has a unique number so we can call them as ascii values so let's say capital a is 65 capital z is some value 91 i guess uh so that goes in that number stream in the same way here we are comparing those two values so yes capital p comes later than capital b so it is greater okay so which is that case can i use a pencil now what do you think which is greater here of course uh pen should be greater than pencil or let's see philosophical way of course because you can't erase what something you write from pen but here if you compare them you will get false why false is because see it will go for the comparison right p p okay same e e same n and same but it will go ahead it will have it has a greater length right see okay so it says pen is over right so we still have some characters here so it is greater so if you do this pen is less than pencil yes and that's true okay that means we can compare a number and a number we can compare a string in a string what up what happens when you have a number and a string let's see what happens what i will do is here the first value we have is let's say two simple the second value is one so what happens when you have a comparison like this one is string of course that's a number but it's a formatting string right and the second one is one uh what will happen now is it the first one greater than second one of course it is because that's two is written one but we have two two different formats here right so in this case it will convert and you can see we got true so it will convert this two into number the same thing can be done for the second one example let's say let's have one more let's try three and this time it should give false because two is not greater than three and we got false that means it is getting converted right so two is getting converted into a number here so this is working okay so now we know how to compare number string and string number with each other so now let's try with a equal to operator i want to check if two numbers are equal let's try so i have three and three and i'm comparing them so what operator should i use i see if you want to check if two numbers are equal we have equal to symbol right so we can simply say equal but unfortunately equal to has a different behavior right so uh we use equal to for assignment operator that example the x is getting assigned with three so that's assignment operator for comparing we have to use something else we have to use double equal to uh so we got double equal to and let's compare what happens and when you compare them we got true simple right okay what happens when you have this in a string format so let's say the first letter the the number three is in string format and the second one is integer or number and we know this right it will try to convert this into a number and let's try so if you're in this code you can see we got true but don't you think that should be false when you try to compare two things and we say it is equal it means it should have the same same data of course it should have the same format or type if it is that it should be numbers it should be string both should be showing then we say it is equal now this thing is actually coming from the start of javascript and it is very common right so double equal to is very common in all the languages and they also check the type because all the other languages or most of the languages are strictly typed language and javascript is loosely typed which means it gets converted remember type coercion it gets converted into the other formats and we don't want that when i say two numbers are equal they has to be equal not just value but the type as well and that's why javascript says use triple equal to i know that's tricky for some people but this is how we do comparison never use double equal to always use triple equal to now you will say why do i have even showed you double equal to because even if you come across that thing you have to ignore that that's the reason i've shown you but always use triple equal to now if you do that it should give you false that's what expected okay see whenever you write a code it's not important if you're getting the output what's important is don't write bugs remember if debugging is removing bugs then coding is adding bugs okay so every time you write a new line of code you may be adding bugs in your code so things like this has to be considered okay this works right so we have to use triple equal to now let me show you something more about double equal to and why it is risky so let's say i have an empty string here and then this is false now if you can see we have two different type of data we have empty string and we have false and then when you compare them with double equal to and if you run this you can see we got true now that's tricky now this is happening because remember we have talked about coercion and we have talked about truthy value falsie value so this is actually false right so empty string is false so the best way to avoid that is with the help of triple e position so when you do triple equal to now it will give you false this is what we want okay so just reiterate when we use double equal to it is only checking the value not the type uh if you are using triple equal to it will check the type and data as well so imagine comparing type off and then comparing the value now in this video we'll talk about logical operators now why do we need them so let's say we have again let's go with the variables let's say we have x equal to 7 we have y in fact we can also use commas here so if you want to define multiple variables we can do that so we can have let's say y equal to 8 and let's say z is equal to 9. so we have these three values right and then i want to perform some comparisons we have seen them right we can use uh less than greater than equal to here i want to perform this operation so let's say i have a data again we can go with the data variable or what makes sense is also use a variable name as result right so i want to compare two values here so let's say i want to compare if x is greater than y now see the resultant of this so if you perform this operation the result would be true or false right it will be false in this case oh we are not printing it so yes it will be false in this case and you can see we got false because this is smaller right okay let's say this is small we should be getting true okay now let's say i want to perform two operations i want to compare two values and then i want to print true what i'm saying is i want this x to be less than y and i also want x to be less than z can we do this can i perform these two comparisons in one line so that's my first comparison which i want it to be true and second one is also i want to be true how can we do that it's very simple just say x less than z so we have x less than y and then x less than z but you can see it is giving you actually others what is that it is giving you some equal to symbol or maybe it is saying you you should use equal to symbol here but we don't we don't want to do that so basically we have two comparisons here and i want to combine them and that's why we have option of logical operators now before going ahead we have to first understand the truth table so whenever you want to combine two comparisons and then based on those two comparisons you want to find the final value so of course right this will give you a different boolean value so this will give you one boolean value and then this second one will give you second boolean value so if both both are giving you true or false or true or false i want to combine them now based on that we can use certain operators here we can use and operator in mathematical terms we we use a dot operator there we can also use all operator so this is referred as plus so you can use plus and then we can use not operator and this can be used with exclamation so we have dot plus or not so this is in mathematical forms so yeah so in mathematical we use till design if not wrong so basically we use those symbols there right now what's important is in programming especially specifically in javascript we use ampersand symbol here and then for all we use a pipe symbol and for not we use exclamation so these are these are the symbols we have to use but what is and or and not so let's talk about them so if you look at this table now so let's say we have two comparisons and each will give its own value so instead of going for comparisons let's go focus on the values so let's say we have two things we have true and true now when you want to combine them based on certain conditions you want to combine them example let's say i want to go live okay so i want to go live on youtube what are the conditions to go live on youtube the first condition is it should be sunday okay and the second condition is i have my camera charged okay so i have these two conditions it should be sunday and i should have my camera charged if it is not sunday and even if my camera is charged i will not be going live second one it is sunday but my camera is not charged i cannot go actually live on sunday because i don't have my camera charged my battery is not charged third situation it is sunday and my camera battery is charged the fourth condition is not sunday and not even my camera battery is charged right so i will not be going live so i will go live the if the output is true so how it will be true if both are true right so it is and condition so it should be sunday and my camera battery should be charged so that's the and operator right so we use and operator there how about our operator so let's say this sunday i know about it i just have two other comparisons now what if my camera is not charged maybe i have a phone which is charged right so i can go live using phone as well right so either my camera has to be charged or my mobile phone has to be charged so we are using our operator right so let's say in the first condition both are charged awesome i can go i can use anything i should go live on sunday and we know it is sunday today right next my camera is charged my phone is not charged i can still go live my camera is not charged my phone is charged i can still go live both are not charged i cannot go live that is all operator either of one has to be charged so what is not operator here so not operator will simply take the input and it will reverse it if it is true it will give you false right so that's how we can use it in fact you will understand the actual use of it in the upcoming videos right but as of now remember this okay so we do this with the help of truth table which you just saw on this screen so in this case i want this both to be true if you want the result to be true in that case you will be using and operator but which and so basically and will also work with bitwise again we have not talked about bitwise so we will talk about that later as of now if you want to use and we have to use two ampersand because one ampersand is dedicated for the bitwise operators so to differentiate between bitwise and comparisons we use double ampersand here okay so now if you're on this code you can see both has to be true then it will give you true and if you run this code you can say we got true but what if the first condition is true the second one is false i will make it five so seven is not less than 5 in this case it will give you false because 1 is true second is false the last result is false but what if i use a or operator here now or says even if 1 is true it's true so the first condition is true the second one is false ultimately you will get true right but yeah what if both are false example i will make this as true now if you can see y is smaller that is smaller so it will be false because both are false right so this is how you can use it okay so result is printing true and false right okay what i will do now is i will just go back here and i will say data is equal to what i will say n and this n i want to store the reverse of results so if the result is true i will say false if the result just falls i will say true how can you do that we can use a not operator so we can say not result so result is true it will give you false if result is positive will give you true and let's let's also print n here so we are printing result and as well as n and if you can see the output the result is false the output is true the n is true right so that's how we can use logical operators so just remember this we have and or and not we will use that in upcoming videos so once we talk about if conditions or for loop we can use this way and in this video we'll talk about conditional statement now see if we talk about the world of programming if we talk about the world of computer everything is based on data and the decisions we take based on that data right so when you say you want to take a decision it's simply based on true or false and we have talked about it right we have talked about the boolean values the true and false and when you compare two values it results in a boolean value okay so that makes sense right but then how can we use that boolean value to take some decisions example if we talk about a flow chart if you talk about the flow of execution it is based on the conditions example if this condition is satisfied i will go on this way if this is false i will go other way round so this is possible with the help of conditional statement so let's get started now whenever you talk about this concept uh conditional statement this is possible with the help of a concept called if else okay okay so there are other varieties as well we'll talk about those things later but as of now let's focus on if and else now how can we focus on that it's very simple let's take an example and doesn't matter which language you learn if you want to learn if else we normally go with simple examples see there is one reason why i'm sticking to very simple examples because we are not here to learn complicated examples we are here to learn a language right so it is easier to learn a language with easier examples so let's get started so what we're going to do is we want to find a given number is even or odd okay that's the thing we do or maybe we can go with even simpler so let's compare two values and we'll print the greater one just to keep it very simple so what i will do is let me take two variables here so i will say num1 is equal to and the value of num1 is let's say 6 and we got num2 and the value of num2 is 4. so we got two variables here i want to compare this two values and print which is the greatest okay so in this case i know it is down one but let's do that programmatically so what we will do here is how will you compare them it's very simple right we have done that before so we simply say num1 is greater than num2 okay and we can save this in a result so we can say let result is equal to num1 is greater than num2 if this is true of course it will give you true because 6 is greater than 4 here in this case of course 6 is always greater than 4 but in this case a number is greater than num2 so that result is getting stored in a result variable and then based on what's the value of a result if it is true i will point num1 is greater okay it's very simple so how do we do that so we have to use a statement called if now if is a statement uh basically you have to pass a parameter which is result so if result is true that's how it works okay it works with true or false so it will check if result is true then execute a statement okay so i want to write a statement inside if condition so let's give a space let's give a tab a proper indentation just to show that whatever i'm mentioning now is a part of if block not a compulsion but a convention good to follow okay so if it is true i will print uh num1 is greater simple stuff right otherwise it will not print anything and at the end i will print by okay again a semicolon is not compulsory but it's a good habit to have semicolon so what we'll do from now is let's get this habit of putting semicolon now if you watch the earlier videos there was an intention of not putting semicolon just to show you there's no need to put semicolons in javascript so it's optional now if you're coming from another language that says cc plus plus java it's compulsion to have a semicolon javascript says it's your choice so from now let's put semicolon it's a good practice okay and at the end we'll print by that's it okay and now let's run this code and see what happens so if you run this code we can see we got num1 is greater and we also got buy okay so how this is working so basically you got initial two values it is checking for the results so num1 is greater than num2 yes result is true so of course remember it will give you a value which is boolean which is true or false and then we are checking if it is true execute this statement okay now let's do some twist let's say this is 3 now we are doing is 3 is less than 4. so 3 is less than 4 now we are comparing the first value and the second value of course this will be false because 3 is not greater than 4 result would be false so if false so what happens is if the value here is false it will not get executed so the block inside if will not get executed okay so let's see what the output is so if you can see the output is simply bi now why we got by it's because it is outside if condition this is outside the if block so that's very important simple right so the if block so this is the block here if this will get executed only when if this is true okay in fact what you can also do is you can write okay we'll do that later uh what if i want to print num 2 is greater than num 2 is greater in this case num 2 is not better right or it is it is greater so i want to print num 2 is greater in that case we can also apply if condition and we can check result so in this case we can again apply a if condition but that doesn't make sense right why to check two times we know if number is not greater it is not to so in that case we can say else so else is another block here and here we are printing the num2 is greater simple and let's see if this works let's run this code and you can see we got num2 is greater see it doesn't matter what's the result the bar will get printed because it is outside the condition so these two statements are depend on the condition right so if it is true it will print the first block if it is false it will execute the second block so else will get executed only when the result is false okay make sense right okay this is cool uh but can we write this condition inside see ultimately if only checks for true and false right so why to create a new variable and why to do those things we can simply cut this part and put it here so we can actually perform the operation or we can evaluate the expression inside the condition itself that's what we're doing here so we are solving this expression here which will give you true or false if it is true it will execute the if blocker is false it will execute the else block that makes sense let's go forward now what if i have two statements here example here i will say i want to print yay okay so the moment you get num 2 is good i also want to print yeah i know that doesn't make any sense but let's say you have a thing where you want to execute multiple statements in the if block or else block will it work let's try now if you can see i am following indentation here so what is indentation is giving a space or a tab after that in every block okay it makes your code look good okay so here what i will do is i will restart on this code and you can see we got num2 is greater yay and by does that make sense right because 4 is greater but what if i will just make this 6. now in this case 6 is greater so what it should print it should print num1 is greater that's it it should not print yeah it should not print num tubes gutter because that's a part of else block let's see if this behaves the same way we are expecting so if we run this code oh that's weird if we got that one is greater that makes sense but why we got yay yes should be a part of the else block right now what is happening is by default every condition let's say if or else every block will have only one statement oh that's tricky that means else will also have this one statement the a part belongs to the the outer block not the else block how do we put that in this block so we have to mention hey both the statements belongs to else block in that case you can use curly packets open and curly package close so now what you are saying is this bunch of statements here belongs to else block the same thing can be done for the if condition and so that's the if block so this is the if block and this is the else block so the currently brackets is optional when you only have one statement so if you have multiple statement that's why you have to use uh the curly brackets now let's try and you can see we got now is greater we don't have a because that's a part of else block make sense cool so that's how we can use if else uh conditions now let me add a twist here the twist is now instead of having two values let's say we have three values we have done that before okay but let's do that so we'll say num3 as well so we got num3 is equal to let's say 7 and i want to check which of this or i want to find the greatest number of these three values so when you want to compare these three values what we will do here is we'll compare the first two values here which is now 1 and num2 and then we have to also compare the num1 with num3 right that's where we will know that num1 is a greatest number so basically here in this if condition we will not just compare number and num2 let's also compare num1 and num3 but the thing is when you have two conditions and we want both to be true which logical operator we are going to use so pause this video in the comment section let me know which operator you think is logical or logical operator which is logical here okay so i hope you answered let's go with the and because we want both this expression to be true so we'll say num1 is greater than num3 so we have to compare both if that is the case will print num1 is greater that makes sense right okay but what a for the second condition so let's say num1 is not greater what next then we have to compare num2 if num2 is greater but then do we okay how do we compare that so in that case we can use something called else if okay so when you again want to compare after the first if condition you have to use else if and this is where you will put the condition so what condition you have to put you have to say num to is greater than num1 and num2 is greater than num 3 cool now once you check that so now if after this of course we know that num 2 is greater so it will print num 2 is greater i don't want to print e anymore it was just for the experiment and once we do that what about num 3 because what if num 2 is greater so in that case you have to put else block here where you will mention log num3 is greatest in fact it should be greatest right we are comparing multiple values okay so that's how it works right so first we are comparing if number is greatest second then we are comparing if num 2 is greatest see if now we know that num 1 and num 2 both are not greatest of course it is num 3 and that's why we are printing num 3's get this here let's try if this works let's run this and you can see num 3 is getters that is right what if i make num2 as 9 in this case num 2 is greatest what if i make num 1 as 10 now is greatest so this works there is one more thing i want to mention is when you have so see if in the first if we are checking if num one is greatest right if this is false that simply means num1 is not greatest then the only competition remains between num2 and num3 then why to even compare num1 and num2 again so the first condition is actually not required we can directly compare num203 and then in the s block we know it is num3 so that's how we can use if else if else so first we talked about if then we talked about if else and then we talked about if else if else so i hope that makes sense uh if you have any more questions let me know in the comments section and let me give you an assignment the assignment is you have to use if else of course but then for the given value you have to find if the number is even or odd try that and answer that in the comment section in the earlier video we have talked about if else right so we have seen if else and then we have seen if else if else now in this video let's go beyond e files and let's talk about another way of checking for the conditions and assigning value okay so what i'm saying is let's say i have a different way so these are the last example let's remove this and let's have a different scenario this time so what we will do is i will find a number is even or odd i guess that was your assignment question right let's try that here so let's say we have a number you have to find if it's even or odd and the result has to be stored in a variable and you can print it later okay so what i'm saying is let's say we have a variable which is num and the value for num is 5 and we know 5 is an odd number but then there's a way you can find that right there's a formula which you have to run so normally in your brain you do that by default but here also we have to run a formula one you have to run a calculation to find okay now if it is even i will save that in a string called let's say result so initial result will be undefined of course right if you don't assign a value which is undefined so here if you try to print result it will be undefined okay so based on the value of num i want to assign is it even order now the way you can do that we have done that before right so first of all how do you find uh even odd number so if you divide a number by 2 and if you get the remainder as 0 it's an even number if you divide the number by 2 and if you get 1 it's an odd number now in this case let's try that so we'll say if and we can write the statement in the if condition itself so we'll say if num mod 2 that's how you do it right when you want to find a remainder you say mod 2. and now you want to compare this with a number zero or one so we'll start with zero i will say equal to equal to so remember we have to use three equal twos if you want to compare so we'll say equal to equal to zero if that is the scenario uh you will assign the valid result right so you will say result is equal to and you will say even right so that's a string you want to assign to but what if it is false in this case you will say else and you will say result is equal to or so if it is true you will say even if it is false you will say odd that's the only thing you want to do right now once you have assigned the value at the end you can come back here and say log and you can simply print result that's the simplest thing you want to do in fact you can have return the log inside evals but this is one of the way you can do it right now once you've got your code let's run this so if you're underscored you can see we got order that makes sense right because five is a odd number but what if you get a number let's say eight now in this case it should print even so this is working now if you want to achieve the same thing there's a shortcut to it so if you know that you just want to compare condition and based on that you want to assign either even or odd so not just for even odd but if any scenario where you have a condition and you have two options either you can go with this or that in that case we can also use a ternary operator this looks like this question mark and colon okay so how do we use this so what i'm saying is instead of using the entire stuff so i don't want to use this i will use a comment so using double slash you can give a comment okay so instead of using this i just want to use a special operator or ternary operator which you can call is question mark and a colon now how do we use this see ultimately i want to assign a value to a result right so if you look at this effects the idea is to assign even or odd to result okay so i will do that i will say result is equal to okay so i want to assign either even or odd so how do we define that it is even or order so i want to assign result it should be even or odd based on a condition and you can assign the condition here so you can put a question mark before it and before the question mark you have to assign your condition so this is num mod 2 is equal to equal to equal to 0 that's it so we are checking for the condition if this is true it will execute the block after this question mark so this will be executed if this is false the condition is false in that case it will print the thing after the colon which is the order okay and let's try so you can see that how many lines we have saved if i remove the entire comments okay let me just reuse seven and you can see we've got order so you can see the code here so we got uh if else which takes four lines and you are doing that on one line that's the power of a ternary operator so that's it from this video i hope you enjoyed that's how tony jupiter works in the next video we'll see something else till then hit that like button and do subscribe for the videos bye now in this video we'll talk about a switch case now what is switch here so let's see an example here so i want to print at what time or maybe i want to set an alarm so based on a day example on monday i want to wake up at 7 00 am on tuesday i want to wake up at 4 00 am so as a trainer i have a contract with different companies and based on that i have to take a training for different countries and we have to get up at different times so when i'm taking training for the company in uk i have a different time zone right i have to wake up at different time so when i take a training for singapore badge i have to wake up at different time so the alarm changes per day right that's tricky i know but i want to create an example here where based on the day i want to set an alarm example on monday i want to wake up at let's say 7 am then on a tuesday because my bad starts from tuesday to let's say thursday uh it depends actually but let's say till thursday i want to wake up at 4 00 a.m because my bad starts at 5 and and let's say on friday i want to wake up because while i don't have any work to i will get up at let's say 9 a.m and that extreme and on sunday and saturday sunday i have a youtube work so i will let's say i wake up at 8 00 a.m it's not a hardcore day so i get up at 8 anytime on weekends so this is my schedule right so i just want to wake up based on this okay uh so how do i do that of course we will not be building an app for alarm here which is a very big application here we just want to print at what time i should get up based on a day you pass example if i pass a day here and if i let's say the day is um if i pass a monday it should print 7 a.m if i pass let's say friday it should print 9 a.m ok that's the only thing i want to do so what do you think what should be done here now one thing we know we can use an option of if else right so we can check for each condition so uh we'll check if the day is monday we'll simply print 7 am but do we have a better option from that in fact what i would suggest is pause this video complete this example with the help of e-files comment your answer and then we'll move towards the alternate even if you don't know how to solve this scroll through the comments someone would have commented the right answer using e fills refer that and then we'll see the solution so the solution is not to use if else of course that can be done but we will be using a special thing because any files you have to apply multiple conditions right because if you observe we have to compile each day then you have to print instead of that we can simply use a thing called switch now in switch again a new keyword in this switch you have to pass a variable in this case the variable for us is day so we are passing day and it will open this block now based on what day you have passed you have to print it right so it will check for multiple cases okay comparisons so we'll write the first case here the first case is let's say monday of course i'm interchanging double quotes single quote but that's that perfectly works if the day is monday in this case i will print 7 am as simple as that okay and then what if the case is let's say tuesday in this case i will simply say tuesday and then we'll print i know it will take a lot of time to type this so let me just complete the entire stuff and let me just fast forward that okay so let's say this is the entire scale right so based on the day i have entered all the options here right so it is friday it will print 9 a.m so i just want to check if this is working so how it works is whatever day you pass it will simply take that day in this variable and then it will it will try to match so whatever you have passed will match with the case okay uh will this work so what we'll do is starting with monday let's start with sunday because that's how we start our week right okay i know that's weird but let's do that so let me just print this quote and let's see if this works if i run this code that perfectly works you can see on sunday i'll be reading at 8 00 am that is working but how about if i make it monday now that's tricky uh it's because if you see the output it is showing all the alarms here seven am four am nine am eight am why is it because that's how switch case works the moment it tries to match in the earlier case it was trying to match with saturday right so when you say saturday it will check for the first case is it saturday of course not is it matching with tuesday no is it matching with wednesday no it will go ahead go ahead at the end when you say sunday oh it was sunday right so it will match with sunday and it will print 8 00 am now since none of the cases matches at the top it will go till the end it will print 8 am but in this case we are saying monday right so it is matching at the start itself once you match the case it will simply fall through it will print all the consoles it will it will simply say hey i got my match let me print everything which i can see on the screen or in the blog in this case you have to mention hey once you are case matching is done and once you feel hey you got monday that's great just get out of this case okay that's you have to mention just get out so after every thing you have to mention that we'll do that up to some time so what i will do here is i will simply print and you can see we got 7 a.m so what is this break so break means jump out of the block so you can see we have switch and when you say match once it is matching it will go to break and break will say hey get out get out of this block and directly you're jumping outside the switch case but yes if you print something here and we can say buy or we can say get lost just for fun and if i run this code you can see we call it 7 a.m get lost instead of this we should say bye so buy will get printed because it is outside the switch block so just printing 7 am it will print by and you can see we got it but now let's move towards tuesday wednesday and thursday if you can observe the block inside each case is same right so when you have the same block what you can actually do is you can skip this part technically we should write break in each one as well but since we have the same thing we can just mention once and that is in thursday so what it will do is it will simply take for tuesday if it is matching it will fall through it will reach till thursday now in thursday we can put a break so that we it will not fall through for friday for friday also we can mention break because saturday is different and for saturday center we have same so we can just remove this and we can just get this for in fact we don't have to mention break at the end because we know it's the end let's try we got 7 am let's try it for let's say wednesday and you can see we got 4 am right so tuesday wednesday and thursday it's 4 a.m so that's how we can use a switch blog now there is one more what if i entered a day which is wrong what if i say holiday now in this case you can see it is not matching with any of the day here so we can write a case which is a default case so default simply means if none of this matching then you can go for default and default you can say sleep or we can say seven am and what the disco videos i know that's where who will watch the videos on holidays but if you want to improve yourself that's what you do and then we have to also put break on sunday because we have another case after sunday so let's run this corner you can see we got uh so when you say it is holiday it will print seven a.m watch telescope videos cool uh so that's how we can use a switch case now in this video we'll talk about a concept called template literal so we'll talk about what is template literal later as of now let's focus why do we need it so let's say we have two variables here which is num1 is equal to 7 and we got num2 is equal to 4. now in this case i have two variables and i want to perform a very basic operation which is addition and we have talked about it so i don't want to spend much time here so it's a result is equal to now 1 plus num2 so once we got these two values and then once we got a result i just want to print a very simple statement and i want to print the addition first of all let's pin the result here so just to understand if things are working certainly print result and if i go back here so let's run this code and let's say app.js and you can see we got 11. so this is working okay but the output i'm expecting here is the addition of seven and four is eleven okay so basically the statement i'm looking for is let me remove the result from here so i just want to print the addition of someone the addition of seven and four is eleven so this is what i want in outputs what i will do is i'll just reduce the size okay so from this code you can see we got the output the addition of seven and four is eleven but the problem is i am using this value as it is right what if in future i am changing the value from seven to nine in that case it was still point seven and four we don't want that we want this value to be coming from the variables so can we just simply use this variable name here so can i say num1 and num2 and here initial using 11 we should be saying result right but will this work let's try so if i run this code for this example what i will do is i will just keep this at the bottom okay so let's run this again just to show you what is happening so you can see we got the addition of num1 and num2 is resolved we don't want this we want the values how do we do this uh there is one way you can use concatenation but the best way to achieve this is with the help of a template literal but let me show you how competition works with so basically you have to make sure that you don't write num1 and num2 inside the double quotes that's the main issue it is printing num number number as it is okay what we can do is after this off we can use a plus operator so we have to combine multiple strings we got num1 again we'll give a plus just to separate it and we'll say and we'll give a space and we have to say again double quotes and then num2 and then double quotes and then is double quotes plus result so whatever i did now i did it intentionally i know you might have missed some double quotes and plus it's very tricky actually and after doing all this trick if you underscored we got the output right this is what we are expecting but don't you think it is a bit complicated so we have a simple solution to it the solution is is template be literal uh the way you can do that is with the help of so instead of using this statement we'll use a log and we'll write so whatever you mentioned now will not be in double quotes a single quote but what you will have is in backtick now this backtick is a symbol which you can find above your tab button or below your escape button so we got backtech in this backtick you have to mention the addition of num1 and num2 is resolved and that's it it will give you the output which you want let's try oh it's not working so the problem is how it will know that number is not a name or it's not a string it's a variable to mention that we don't want number one right we want the value of num1 to achieve that we have to use a dollar symbol and a cuddly packet and you can see the color has changed so this is different this is different the same thing can be done with this a dollar symbol and a curly bracket the same thing for result or dollar symbol and the curly bank and that's it so now what you're doing is you're saying the addition of then the value of num1 and the value of num2 is the result let's try and record it it's working right so that's how you use a template lateral okay there's one my advantage now since it is template literal it will literally print whatever you mention example let's say i will say log and i will print my name is naveen ready but i want to print navin ready on a new line so what you do normally is you say slash end right which is new line and you can see we got the earlier output and we also got my name is ready now we need a new line instead of using slash in what you can actually do is you can use a backtick here and don't even have to mention slashing it's just that right now and ready on new line okay so it will print as it is the way you want and you can see we've got manuscript ready in this video we'll talk about loops now see we'll divide this into two parts in the first video we'll talk about what is loop and what are different types of loops available and in the next video we'll see which loop to use at what situation okay because that's a tricky part we have three different loops to work with okay so again we'll talk about what is loop first but then we have three different loops and people do get confused between which one to use when and that we'll see in the next video so if you know loops and you just want to know which one to use you can directly jump to the next video but if you want to understand how loop works let's get started so basically in javascript we have three different type of loops we have a while loop we have a do while loop and then we have a for loop again what is loop we'll talk about it as of now let's start with what is loop so let's say i want to do something repeatedly and that's what loop means right you have to do something repeatedly example every day you take a bath every day you work every day you eat twice in a day but you do that repeatedly right every day that is what we call as a loop right so let's say i have a log statement here and in this log statement i am printing hi now if i want to print hi only once i can simply write it once right and then when you run this code of course you will get high once what if i want to print i want to execute this statement three times or five times okay three times is easy you can simply copy paste this you can say copy paste and paste of course you will get this three times okay so we got three times right what if you want to do this five times of course you will say hey copy paste two more times what if i do this 100 times 200 times thousand times of course this doesn't make any sense right and then we have a very famous principle called as dry which is do not repeat yourself don't write the same statement multiple times can we just use one statement and run it multiple times we can simply say hey you know we can just say let's keep that in a block okay so that's how you get a block right so let's put that in a block and say hey repeat this statement five times that's it if you do this and if you run this code you got high only once you know what happened is because we have created a blog but then we have it a comment here javascript will not even execute this comment but if you write it without comment of course this is not logical statement or a syntactically correct statement right you have to write the same thing but in a proper format which your javascript understands so to write this we have to use a type of loop so the first type of loop we are going to use is a while loop now see when you say repeat this statement five times you need certain things here first how do you know how many times you have done that so first we need a counter right so once two times three times four times five times so we need a counter here and every time you have to increment this counter you have to say one two three so you have to go ahead right and then you have to also check what is the current value because you don't want to go beyond five you don't want to go to six or seven eight times you want it only five times so we need three things the counter you have to check for the condition and we have to also increment it right okay so three steps so we call that as initialize and then the next step would be condition and the third step would be increment right so we have these three steps so the first step is you have to first create a variable which will act as a counter so we'll say we can use any variable name doesn't matter actually but logically let's go with i because i means increment okay so we'll say i equal to initially 1 that's how you start okay and then uh if you remember last time we mentioned we'll use semicolon at the end now and then uh we also need to check for the condition now can we use condition like if yes we can do that right because we want to just for the check for the condition so we can use if but then if will exit only once right that block executes only once so if you say if i should be less than equal to 5 because that's the limit i want to go for but then if will exit only one so instead of using if we will be using while because while is a loop it will get repeated so you can see that while english word which means repeat we have to check for the condition okay it will execute this every time this condition is true but then we have to also make sure that you also increment so example when you print high once you have to make sure that the i value goes to two so that it will again print high then to make sure that the i value goes ahead with three that's how you do right you say hi hi hi hi hi so what are you doing here you're checking for the you're incrementing it you're checking for the condition why i stopped it here if i have not went for the sixth number because condition okay so we'll say i plus plus remember we can increment a value with the help of i plus plus you can also say i equal to i plus 1 but then we have seen this right this is amazing shortcut any guess what this is called use that comment section and answer that what is this called okay and the answer is it is post increment we can also use spray increment here because when you only have increment not assignment both behave the same way okay will this work let's try so let's go back here and run this and you can see we got high five times it's working right that's how your while loop works so basically we have three things here we have initial value we have a condition and we have a increment or decrement see there is no compulsion that there should be always a increment okay there is a different way you can do that example if i don't do increment here that's fine you just have to change this condition maybe there has to be some other way of checking for the condition because if you don't increment it will not go ahead and it will simply say true for the entire lifetime in fact we'll make sure that your machine hangs if you do that so let's not do that let's do this three steps so that's how your while loop works right next we have one more loop which is called do while loop so what do while loop does is we have okay there's one more thing i want to show you let's say the value of i initially is 6. now what will happen when your value of i is 6 and then it will check for the condition right is 6 less than 5 no it's false the moment you get false it will directly jump out which means the block will not get executed even once let me show you that so if you go back and if you say run we have not got any output because the block is not getting executed okay so if you go back to one now let's see what is happening let me just run this code you can see high five times the i value is one here it will go inside it will check for the condition is one less than five yes it will go inside it will print high so we got high once and then it will say i plus plus that means it will increment the value of i now the value of i is 2 so it will check is 2 less than 5 yes it will print high and then it will go ahead it will make i value 3. again it will repeat it will make it four and then five at one point the condition will be false and it will come out that's how it works now the question for everyone and the question is what if i print the value of i here what is your guess what will the value of i so again pause the video and since i'm asking multiple questions in each video now you can specify the timeline so that it will be helpful for others so we are printing i what will be the value of i here i hope you have answered that and the value is 6. now that's weird right we are saying the value of i should be stopped at 5 see what is happening is after several increments the value of i will be let's say 4 okay in this case it will print high it will increment the value the value of i becomes 5 it will go up is 5 is less than equal to 5 yes it is equal to 5 it will print high it will make the value of i 6. okay then it will check is 6 less than equal to 5 no that's false so we got false but what's the value of i it is six right and that's why we got six cool okay one thing to remember we have three steps here initialization condition and increment okay you can also do reverse here okay example if you want to start from six or you can start from five so this time we are doing reverse in fact what i will also do is before doing that let me also print the value of i so it will print high one high two and let's do that and you can see we got high one high two high three high four and high five oh that's one that's amazing high five okay but then what if you do reverse so i can say i equal to five and then i is greater than or equal to zero because this time we are going in reverse order so i will go from five four three to one countdown and then instead of using increment you have to say decrement because every time you want to reduce the value of i five four three two one let's try and we got it so you can see we got five four three two one zero that's tricky we got six it's because it is five four three two one zero okay so we should not be going till zero we should be going till one so ever you can make this as one that's one way of doing it or keep it zero remove this equal to symbol so when you say i should be greater than zero it will stop at one okay so you have two choice let's run this and you can say we got five four three two one cool so that's how we can do increment and decrement okay but one thing to observe what if you say uh let's say this is 0 okay okay let's keep it is 5 and we'll go back to our original way which is less than equal to five and we'll say plus plus so in this case of course we are going for increment basis now and let me set this value of i here as 10. now what do you think it will even print it once and then says no it will not print you one once right because that's how while loop works so while loop will first check for the condition then only it will go ahead so what if you want to just execute it and then check for the condition so in that case you can use do while so in do while what you do is you check for the condition later so after the curly brackets at the end you will check for the condition now what it will do is it will say okay the value of i is 10 that's fine do says okay go inside it will check okay we are printing the value of i and then we are saying i plus plus it will make the value of i as 11. it will come down and say okay 11 is less than equal to 5 no just go out of the loop that's it but it will print the output at least once that's the thing behind do while but yes if you start with one there is no problem it will behave the way it was behaving before so do while and while they're almost same the only differences so in while you first check for the condition then you print the block or you execute the block in do while this reverse first it will execute the block then it will check for the condition and in this video we'll talk about for loop so in the earlier video we have talked about while loop we have talked about do while loop why do we need another loop here see the thing is let's go back to while loop just to understand uh so let's put the condition which is i less than equal to five and let's remove the while loop from here okay so this is your while loop right a simple while loop so here we can see we are doing three steps we are doing initialization we are checking for the condition and then we are doing increment and normally when you want to do some increment and if you want to run a loop for a specific number of equations what makes much more sense is to write these three things on the same line don't you think if you just want to iterate between the numbers this will be same i mean the value of i will be assigned you have to check for the condition you have to increment or decrement based on the situation so here what we can do is instead of using a while loop we can use a for loop now how it looks like so instead of using while we'll use four the only thing is in for loop you do all these steps in one line example so remove the condition you use two semicolons here the first we are creating three statements with two semicolon in between the first one is uh let i equal to one that's the first step initialization the next step you have to do is for the condition and the third one is the increment which is i plus plus so by doing this you don't have to mention this let you don't have to increment so we are doing everything in one line mind you it is still three statements because you are using semicolon at the end right with the fourth separation but the line is one and now you've run this code you've got the same output right this is what we wanted we want to print high five times but instead of using while do while we have used a for loop quite simple right now the only thing is is it compulsory to write everything in one line see the option is you can write everything in one line but if you choose you can still go for the old pattern that means you can achieve while repeating for loop right example let's say i don't want to initialize the variable i will instruct here button in the for loop i will not initialize i will just check for the condition and increment it work let me show you you can see we got the same output but what if you don't even want to do increment here we can do the increment later in the block and even that works okay so there's no compulsion that you should write everything in one line for loop offers that but you can skip that in fact you can also skip the condition okay i know that will be weird because there's no condition it will go forever that's we called as infinite loop look at the output here it's crazy okay let's see where it ends so just fast forward this video and you know the speed is actually depend upon your cpu let me just check the task manager how much this is taking uh maybe later on we will try with some other weird stuff just to see if we can consume the complete memory look at the visual studio processing it is taking 54 percent of my cpu it's actually very time consuming so i'm just stopping it here you can say control say it will stop the execution so cpu is under control now uh so that's the thing you can just try it out i'm not sure at what point it will end i've never tried it so you can try on your machine but again it's not compulsory you have to put the condition if you want to stop at particular point so we want to stop at i less than equal to five and it's always better to initialize the variable inside a for loop and do the increment and decrement inside this space here which is i plus plus i will select i equal to one so that you don't have to mention that here but now the question would be which is the better loop should we go for a while loop or do while loop or a for loop and that will discuss in the next video so as of now we have understood what is while loop do while loop and for loop in fact you know you can actually use a loop inside a loop example i want to print high and then i want to print hello five times then high and then hello five times so you can actually do that example what i'm doing is i just want to say log and i want to print hello here right so it should print high once and then five hellos and then one higher than five yellows so basically in total i want to execute hello 25 times but then it should print one high and then five hellos right so what we can do here is we can write this with another for loop so basically we are using a loop inside a loop which is called a nested loop so let's try so let's say j equal to 1 j less than equal to 5 and j plus plus right and there's one more thing if you know that you only have one statement inside your blog example if you look at this folder we only have one statement so the currently package is not compulsion though we have done that in if and if and else right so if you have only one statement curly brackets not required okay let's try in fact i will also print the value of j and let's see how this behaves and i will go back here and let's run you can see that okay that's the other output we got so we have high one and then it prints hello five times you can see the value of j is incrementing and then you've got high two here and then we got hello five times then high three hello five times so what is happening here is if you just try to run this code mentally you got the value of i which is initially one it will check for the condition it will not to increment there it will just print this i value then it will go for the next loop which is j the value of j is one and then it will print hello one so just printing high one and then hello one that's what you've got the output here the first one and then this loop will get continue okay it will not jump to the outer loop this is a inner loop it will first complete the inner loop then it will go for the outer loop so it's changing the value of j to two and then it is printing hello two then you're sprinting the value of j to three they're spinning hello three and it is reaching till five it prints hello five it is the value of j is incrementing to six now it is a false condition right it will jump out of for loop it will go for the outer loop and the value of i is 1 it will make it 2 and that's where you can see we got high 2 but then the moment you go back to this loop the value of j will be reinitialized to 1 and that's how it works example you have an ear let's say year 2021 and in this year you have 12 months right so january february march april and then it will reach to december because of next year 2022 but what will happen again january will start that's how it is working now in this video let's try to find which is the best loop now first of all there's no best look okay a lot of content online by saying for loop is the best loop that's not the case every loop has its own advantage and a drawback okay and they can be used in special cases example let's say see when you know the starting point and ending point we will always go for for loop it always works perfectly so if you know the starting value you know the ending value you know the equations for loop is the best example if you want to print something from 1 to 100 photo is the best right even if you want to print all the numbers between 1 200 which is divisible by 3 you can use a for loop let's do an example here so we'll use a for loop and we'll start with let i equal to 1 i less than equal to so we have to go till 100 right and then we'll say i plus plus now here i don't want to print all the values i just want to print those values which are divisible by three now how do we do that so what i will suggest you is uh pause the video try it by yourself that's how you learn right so pause this video try the example from yourself and execute that tell me the output in the comment section or the quote in the comment section and then continue okay so i'm not sure if your quote is right which you have commented but let's try so what we will do is we'll check so every time you do the iteration you check if the given number is divisible by 3 you do that with the help of mod 3. if it is divisible in that case you will print log and you will print the value of i by doing this you are just printing all the numbers which are divisible by 3 okay so you can also do it for even numbers and other stuff maybe you want to print all the prime numbers you can do that so you can see we got all the values we got 3 6 9 12 so this is the entire table of three till 100 okay so when you know the starting point ending point for loop makes sense that's the first use case right so what i will do now is i will just comment this part and let's understand where while loop is the best scenario see you can do the same thing on using while loop as well but let's say i want to print all the numbers individually example let's say we have a number here and then i have a number which is one two three four five six okay now this is one single number of course you can change this number by any format but let's say you have this one single number which is one two three four five six and i want to print each digit individually how do we do that uh so basically first of all how do you break this entire thing okay so let's print in reverse order first let's print six and then five then four then three then two and one first of all how do you get six and we have seen this right we can divide this number by ten the remainder will be six right let me just do that so it's a log and i will print num mod ten so when you say num mod then you can see we will get 6 okay that's simple but how will you get 5 now in that case what we will do is we'll break this number and we'll remove 6 now how do you remove 6. it's very simple if you say num is equal to num by 10 now what you're doing you're dividing this number by 10 okay in this case the num will be one two three four five now example let me print num so i'm just printing num now and if you see the output you can see we got oh it just being the floating point okay so how do you solve this problem so basically what we are getting is a floating point right so there is one way uh you can actually get the percent of it remember we have done that you can convert a float into integer with the other percent and you can see we got one two three four five cool and then if we repeat this steps multiple times so what we will do now is let's put this together and let's do this multiple times copy paste paste so you can see i'm doing that for three times and see the output we got six five four and if you do this multiple time you will get three to one also okay that's cool right in fact you can also change this number there's no compulsion that you should have the same number what if i do randomly uh this so if i choose this number you can see we got two last number then eight then seven and then if you do this multiple times you will get four six and five okay this looks cool right now what i will do is instead of doing this multiple times we know we should be using a loop but the question is which loop now you will say for loop right but then we don't know the number of digits we have in this number of course you can find a length but that's a complicated stuff so we can simply use a while loop and in this what should be the condition see every time you divide this number so you are reducing the number length of this thing right so you have to make sure you have to continue this till your value of num is greater than zero if the value of num is going less than zero then you have to stop that's it right so we don't have the number of digits counting here we're just going for a condition in this case while loop is the best one right so if you now if you can see we got 2 8 7 4 in fact i will not print this part i just i don't don't want to print num but that's where it is creating that issue so if you can see we got all the digits here that's how we can use a while loop right so while loop is better when you don't know the starting point ending point but you know the condition when to stop right so example if i say hey count till 100 so you will say one two three four but what you say you have to start counting from one and then you will start the counting write one two three you don't know the ending here if i say stop then you have to stop so the first scenario when i ask you to count from 1 200 for loop when i ask you to keep counting while loop okay and the moment i say stop you have to stop so instead of using while you can also use do while the only difference between while and do while is while we'll first check for the condition then execute do while will first execute the block then check for the condition okay example you want to chat with someone so if you want to end the chat i mean the chat will get injured when you say stop or end the chat while loop right continuous continuous stuff do while is even if the person is offline you will send the first message and then you will get the message the person is offline at least you have sent the first message that is do while loop okay so that's it that's the comparison between while and do while but let me give an assignment here now since you have done this since we have done this example what i want you is if i give you this number you have to print the reverse of this number okay so at the end let's say you got num2 maybe let's do that here let's say num2 and the initial value of num 2 is 0 at the end when you print num2 i want you to print the reverse of this number which is 2 8 7 4 5 6 or sorry 6 5 so that will be assignment in this video we'll talk about objects now see the thing is object is one of the most important thing in some of the languages because it represents the real world in the virtual world example let's say in the real world we deal with object writing we don't deal with normal variables example if i want to talk about a laptop laptop is an object now this laptop will have certain properties right so let's say if i want to represent a laptop in the virtual world i have to define it as one object which will have multiple properties maybe you can talk about the laptop configuration the cpu ram the storage you can talk about the brand in the same way if you want to represent a bank account so a user bank account will have certain properties right so a bank account will be one object or the user has one object and the user will have multiple properties the account balance the username the password and all those aspects so whatever you can think about is an object right so how do you represent an object in javascript so basically we have different ways of getting object okay so we'll go one by one the first one we'll deal with is object literal okay because here you literally mention all the properties of an object okay so what i'm talking about so let's say i want to represent an object this time we'll go with let's say alien so let's say we want to create a object called alien right now this alien will have certain properties so we'll say alien so when you talk about alien what exactly alien means now if you are new to this video or this channel you might not be knowing this but i call my audience as aliens because i believe they live in virtual world right and so they are not from real world so they are aliens right so a alien so every iit professional is an alien right so how do you represent rit professional so of course every programmer you can say is has a name has the technology of course everyone loves some technology right and then maybe the number of experience in that technology so this is how you can define the properties so basically it's a key and a value pair right so we can have multiple properties and each property will be having a key and a value now how do you define them now first of all let me create a simple object without any properties can we do that yes you can simply open a curly package and close it this is your object trust me i know that looks weird but that is your first object now the only problem is you can't actually see the data in it right we have not defined the properties yet but if you even if you don't define that's your object now how do i prove it now if you go back to our second or third session i don't remember when we talked about the data types we have talked about different data types right we have talked about numbers string boolean and then we said whatever is not primitive is an object so of course when you talk about objects they are not primitives in this case so this is an object okay this we have we don't have any primitive value assigned so that's an object right okay let me just print it let me just print it as it is and let's see what happens if you run this code the moment you're on this code you can see we got empty object now how do you know this is an object so we know the special operator right we have type of operator let's do that and run and you can see it says object okay so that's your first object awesome now we know it's an object but i want to assign some properties how do you assign a property okay so since we have a curly brackets of course this makes sense right it's a collection of properties so we can put our properties here okay how do we define this it's actually very simple so when you open the curly brackets in that block you define your properties i will go with the first one i will go with name service name is my property and how do you assign a value to it okay now that's tricky so we don't use equal to symbol we use a colon because that's a key and a value it's a pairing basically so use a name and then you assign a value to it so i will say the name is naveen and then i will give a comma that's how you separate multiple properties now if you are coming from different language let's say you know java so in java we use map concept for this key value then in python we have dictionary in fact most of the languages they have dictionaries so it's almost similar right here let me just define another property let's say technology so my favorite technology is i will say javascript at this point because we are learning javascript let me define that let's go with these two properties in the upcoming videos we'll expand this example so that it will make sense but as of now i just have a very simple example or simple object here which is alien and can we create multiple objects of course we can we can create thousands of objects and that's what we do in real world so when you work on a big project or any real-world project you will be working with multiple objects okay so we got these two properties right and that's your object now let me just print it as it is we are not specifying what i want to print i just want to print the object and you can see you will get the output in that format just do that here okay so we got the output right now i don't want to fetch the entire object i just want to fetch a particular value i want to know the alien let's say name i want to know the name of the alien now in this case of course if you print alien it will print the object as it is i don't want that i want to get the name right so in this case what you can do is you can say alien dot name is that simple you have to use a dot operator okay we have another option but let's go with the dot operator okay so we got it right so we are getting this name here what do you think will it work let's try it's working you can see we got uh we got naveen there okay that's perfect can we also print a technology yes we can let's run this and okay you can see we got the output so how do you fetch a value of a property or an object you use the dot operator now that's not the only option you have you have one more option you can also use square brackets so you can say alien and in this square bracket see because see when you mention alien it will print it will fetch all the properties we don't want all the properties we want only one or two depend upon your requirement so in this case i can mention that in a square bracket you can say hey i want alien data but not all specifically in that square bracket and in single quote you can mention i want to fetch name okay this is how you can fetch the value and you can see we got name you can also fetch the technology yeah so you can also you can see we got js now the question is which is better which one you should use should you use dot operator or square bracket now it depends upon the requirement what makes much more sense is to use dot operator because that defines okay alien dot name alien.tech if you have object inside an object it makes much more sense to use that but then square back it has its own requirement so given a choice i will always use dot operator but there are certain requirements where you have to use a square bracket now what are those requirements let's say if you have let's say one more property here but then the property name has two words example in this case efficiency we have a name we have tech what if you have one more property which is not of one word can we do that can let's say i want to have number of years of experience i can say work experience so now i have a property of two words now see logically you can use underscore here if you if you have two words that's how you define it right just to keep it simple but then what if you have two words and you have to give a space in between in that scenario you can use a single quote again i'm not a big fan of this type of property naming convention but it gives an option you can do that let's say the number of year of experience is four okay so if you have a property name with two words of course you will use a single quote now why i'm not using single quote here it's a number right if you don't use single quote for numbers okay and then if you want to fetch that you have to say work experience you can't use dot operator here so there are certain conditions where you have to you have to use the square bracket actually there is one more what if i have let's say i want to fetch the name okay so of course if you want to fetch the name dot operator makes sense so you will say name here but what if you don't know what to fetch you will be asking the user hey hey user what you want to know do you want to know the name or the tech so let's say user is giving you the input okay and then the input is name let's say that's a scenario input is name now imagine this name is actually coming from the user we are defining it statically here but imagine that's coming from the user and then i want to fetch the name so depend upon what the input is i want to fetch that element can i use dot now of course not if you try to use input it will give you some issue it will say undefined because we don't have input as a field inside alien right so we are saying hey i'm not talking about input i'm talking about the value of the input in that case you can't use dot operator you need to use the square bracket okay you are evaluating the expression to get the value so name is coming here and then you're getting the value i hope that makes sense right yeah so that was the introduction of an object so what is object basically it represents the real entities okay so in this case we are going for alien of course you can make a complex object okay before going ahead before ending this video i just have one more thing in this particular object we have properties right but can we also have functions example let's say if we talk about a alien we have of course we have properties right but then we also have a behavior right we write code we test the applications we give a speech right so we do make a lot of content or we write a lot of code can we have functions inside the object that we'll see later okay short answer is yes but how we can do that that we'll see in the upcoming videos till this point we have talked about the basics of javascript and then we have talked about the object it's very important right object is one of the most important thing in most of the languages the thing is when you talked about objects in javascript we have talked about key value pair so basically you can represent the real world objects in virtual world with the help of this javascript objects here so it will have a key and a value pair right now in this video let's expand this more what if you want to create a complex object now what i mean by complex object so imagine you have an object and then inside that you have another object what if you want to delete a specific property so let's say in your object you have four properties and you want to delete one is it possible what if you want to print all the properties i mean the key and a value is it possible of course you can print one by one but i want to do a iteration i didn't want to print everything by myself manually right so let's find out in this video so the first thing we'll do is let's create a simple object again the same one we'll go for which is alien and then let's make it a complex object so what we will do here is again the same thing we'll create alien object we'll say alien equal to and then we'll mention the properties so last time i guess we went with a name and then we'll say name is here naveen and then we'll go for the next property which is technology and i guess the technology was javascript now since we are learning javascript we can do that okay so let's go with this too uh this time let's ignore the work experience right so here i just want to print this object is it possible of course we have done that right why we have to even think about it so we can simply print the object okay let's try this so if you run this code you can see we got the output which is naveen and js which is the values uh what if you want to add some more properties we can add more properties right okay before that i just want to add one more thing what if you have two properties of course you can differentiate between different properties with the help of comma right so once you complete one property here uh before going for the next property you will give a comma but what if if i give a comma at the end that doesn't make sense right because we don't have any value after that but javascript says that's fine even if you give a comma here that is completely fine you know why it's because sometimes we want to add more properties example if you have a comma here and then you added some more properties and then you realize hey we don't need a large property you will delete it but then you forgot to read this comma right it will lead to errors and bugs so javascript says okay i will understand that don't worry so that completely works let me show you here uh if you run this code in the output it is ignoring it that's a good thing okay let's make this as a complex object now what i mean by complex object here is having an object inside the object so in this alien let's say a area need a laptop of course we can say a laptop here but then a laptop is not a simple thing right you can't simply mention a brand here or a configuration you want to specify everything as a separate object so every alien if you want to work you need a computer of course laptop is not necessary here we can have a simple computer but it works well laptop works well right you can even get it i'm not a big fan of laptops but let's go for a laptop here so here i want to create a laptop object right in this case i will open the curly brackets and close so you can imagine a object as a property okay so the laptop is a key and this object here which is a object literal is a value now what i want to mention so let's say i will go with very simple stuff let's say i want to go for cpu and cpu is let's say i7 and the next thing i want to mention is ram and let's say ram is 4gb and then i want to mention one more thing which is brand so let's say my favorite brand is asus and that's it we got these three properties right so what we have here is we have a laptop inside this alien object so that's object inside the object now what happens if you print this it's very simple you can just simply run this code and you can see we got the entire object cool this is working right now what if you want to fetch only technology we have done that we can simply say alien.txt which perfectly works and let's get back here and run and you can see we got js but what if i want to print on the laptop so alien.laptop even that works so you can see we got cpu ram and brand basically we got the entire object of laptop but what if you want to print only brand here can we do that see it's very easy to fetch a laptop because it belongs to alien so you can simply say alien.laptop how about fetching the brand i can guess it we can simply add a dot and a brand and that's how it works so we can say hey we want brand which belongs to laptop which belongs to alien it's so simple right let's save this and let's run and we got it we got asus that's cool now the amazing thing is every property value for example if we talk about string string has a specific properties okay we can say functions methods but we'll talk about those things later imagine i want to know the length of a string example here brand is a string right i want to print the length of it so basically you can use a property called length uh so basically what we do let's go reverse i want to find a length of what of brand so whatever value brand has let's say asus or lenovo or hp it will find the length in this case it is four if you go for hp it will be two if you go for lenovo it will be six and then where you will find this brand it will it belongs to laptop laptop belongs to alien that's what that's why we represent that dot operator there so this is working let's try to underscore and yeah you got four now four is a length of associate this is working cool but then if you do that it's working right but we have another concept here which is double question mark okay let's talk about that what if you don't have this brand as a property let's say the property name is brand one okay but then you are using a brand so what will happen now see when you try to fetch only the blender what i will do is i will just comment this part and i just want to reuse it okay and then let's say i just want to fetch the brand here now what you tell me what will happen now since we don't have brand it will give you a very special error and so it will print undefined and we have seen this from a long time right every time you try to fetch something if it is not available or the value is not assigned it was undefined cool but what if you try to find the length of brand which is undefined here and if you underscore you will see you got an error now we don't want others right uh there's something which we don't like but you don't have an option right you have to face it now in this case don't you think before fetching the length we have to first verify do we have brand available how do we do that it's actually simple you know before printing the brand you can actually perform the if condition here so you can say in if you can mention alien.laptop.brand is triple equal to undefined if that's the case don't print it but let's not go in that format here in fact you will find some of the answers in the chat window if you have written that code comment your answer in the comment section so that others will come to know the code right but here what i want to do is i want to check see find length only when you have brand available in the object if you don't have brand available don't print the length let's try this so let's run and you can see we have undefined we have not got the error it's because we don't have brand available but what if you have brand available then that question mark doesn't matter right because you have brand available cool what if you don't have laptop available so you can see we have a laptop one here and then i'm trying to print laptop let's run this code and you can see we got error how do we solve this again we'll put a question mark here now if you put a question mark it will say undefined not the error right so this is one of the important thing you can use you can also do it for alien what if alien does not exist so you can put question mark wherever you want uh if you think hey this is a property which i'm not sure of put a question mark so that's how we can print one specific property here so we have talked about question mark we have talked about stuff let's talk about something else let me delete one of the property can we do that i want to delete laptop itself can we do that because if you print the alien object here you can see this printing the alien object but what deleted from the alien so thankfully javascript provides you a keyword called delete and you can mention the property you want to delete i want to delete a laptop so i can simply say alien.laptop and it should work let's try and you can see laptop gone okay it's that simple so we have an option of using delete here which will delete your laptop or delete your property whichever you mentioned uh let's say i want to mention tech it will delete tech for you and that's done you can see we don't have tech anymore okay so that's how you can delete properties we have talked about complex object we have talked about delete we have talked about uh how do you use question mark so let's do the iteration let's say i want to print all these values we'll do that in the next video now in the earlier video we have talked about objects right we have talked about how to create an object and then we have also seen how do you save object inside the object right and then we have seen how to delete the property from the object in this video let's try to print all the properties hey we have done that right we can simply print an object it will print all the properties of the object example let's say if i have the alien object here and i want to print the entire alien object so i can simply print alien and if i run this code you can see we got an object but then what if i want to print this properties one by one uh can we do this and yes that's where we have this loop which is a for loop or not the photo which we have seen before this is a special type of loop which is for in loop okay so how do we use it uh so basically for using a for in low we have to say for the way we do it uh for normal for loop and then we need a variable here but this time this variable is not to a trade between the numbers okay it's not one two three four this variable here is to represent a particular key see the only way to fetch a value of a properties with the help of a key right example if i want to fetch this javascript here we have to use tech that's how it work right so here we need to represent p or we can say key because we are fetching a key of the property uh so we'll say let key in which object you are fetching from the in object so basically this for loop will iterate between the elements or between the properties all these properties here and it will take one key at a time so with that one key let me just print the key and let's see what happens let me just remove this alien part from here so what you are basically doing is we are printing a key here right only a key not a value let's see what happens the moment you're on this code you got all the keys right you got name you got tech you got laptop now the question would be why not cpu ram and brand it's quite obvious right the key for alien is only three these are the keys these are the properties for laptop right uh so that's why we only got laptop not the internal part of it okay but once we have a key with us what we can do with it a lot of things of course we can fetch all the values so let me also print the values here how do you print a value it's quite simple right we can fetch the value from the alien object okay uh how do you fetch a particular value so let's say i want to fetch this js here so the way you can fetch that js is by giving a dot here so we can say alien dot you can mention tech right it will fetch tech and let's see what happens if we do this the moment you run this code you can see we got all the properties that's perfectly works but the values are weird we got name tech and laptop those are the keys the values are jsjs you know why we are tech here we don't want that we don't want to mention one particular property there it should change based on which key it represents so in this case we will be using a square bracket because key is not a part of the object right so we have to use square bracket and we'll mention key here now this key will keep changing right initially this will be name then it will jump to tech then it will jump to laptop and it will fetch that particular value will this work let's try so first of all let me clear it and run and that works you can see we got name naveen we got tech js we got laptop and thing works right now basically when we are using this type of loop we can fetch a key and with that key you can fetch a value right if that makes sense but then what if i want to print all the properties of a laptop now this is something you will be doing by your own okay so that's your assignment uh we have done it for alien you have to do it for laptop and if you are not able to do that that's okay you can find the comment section someone might have answered it okay so i will check your comments and let me know what is your answer how do you print all the properties of the laptop okay so this is something we have done we don't use it often but if you want to print all the properties this is how you do it right now apart from this we also have a for off loop which we'll see when we start with added okay but as of now we have talked about a for loop and that's how you can print all the values of the object now in this video we'll talk about functions see we talked about objects right and then in fact we have seen the basics of object it's time to move towards functions and then later we'll try to merge and we'll try to use this together ultimately when you build projects you need to know everything right you have to use all those features so let's start with functions now what are functions see basically you write a code or you build an application to solve the real life issues right so basically you try to convert the real life issues and then you find a solution and then you solve it with the help of a virtual world and that's where you write multiple lines of code right now this can be 10 lines this can be 100 line this can be 1000 lines of code now one thing is for sure there are certain lines of code which you want to reuse okay that's the first thing you may want to reuse the code okay example let's say you have bunch of statement let's say five statements which you will be calling multiple times maybe this is for a login purpose maybe this is for cleaning the system or this might be for a payment checkout so if you have these five statements which you want to use multiple times you can call it multiple times right that's the first thing second is for a bigger task it's always better to create small task okay so whenever you have a big task break down into small chunks it will be easier to code it will be easier to manage also right and then we have this principle which is called dry which is do not repeat yourself okay so to solve the three issues which we have talked about we have one solution which is to use a function now what are functions uh so let's go with a very simple example here so what we will do here is let me get a function so we'll say function here but then what this function will do so let's say i want to greet someone so how do we grade it so let me just remove this uh how to be great so let's say we can say hello world or a good morning or get lost i'm just kidding so let's say if you want to say hello world we have seen it right we can simply say log and then here we can say hello world that simple right now if you run this code of course you will get hello world so let me just do that quickly so you can see we got hello world what if you want to print hello world five times or now you'll be saying hey we can use a loop there right we can we can but if you want to print five times repeatedly of course you will use a for loop there but what if you want to call this multiple times in a different situation in that case what you can do is you can put this statement not just one statement you can put bunch of statement in a thing called function and in this function you can give a name to it let's say i will say the name is great of course this name can be anything but let me say greet and then you will open a curly bracket and you will close it okay you will put this statement inside the function right okay so basically we got this hello world and then now i want to call this so let me save this and let's run this what do you think will you get the output so in the comment section mentioned will you get the output or not let me just try this and you can see we have not got the output there is no other of course but we have not even got the output see this is a function right so we are defining a function this is what is very important we are defining it so this greek is a function and then we are defining it with the help of this function keyword and then after this grid this is the name of the function you are using this round brackets that's how you define a function and then you have to open the curly brackets close the curly brackets inside this you can write whatever statement you want to execute this might be variable declaration this might be statement execution this might be calling some other function yes we can do that that we'll see in the upcoming videos but as of now i just want to call this function how do we do it we simply call this function called as greet that's it you need to call a function so there are two steps here one you are defining a function and second you are calling a function so you can put a semicolon there it's an optional thing but it's a good thing to do so this is a function definition this is a function calling let's see what happens now in this code you can see we got hello world now the awesome thing is you can just repeat this thing you can say paste paste and paste so now we are calling that function four times now this is not exactly the same as for loop because in for loop this will execute it in a loop maybe after this grid i can write some other statement okay so example let me print one here so now what will happen is it will call greet it will execute hello world then it will print one then it will call greed again and then again and again so basically it goes in a flow right so it will get called every time you write this great and uh run bracket so let me try that and you can see we got the output which is hello world one hello world hello world hello world it's that simple right so that's how we can uh create a function we can call the function so this thing is a function definition we are here calling it multiple times but then how exactly we can use it for other purpose what if you want to execute bunch of statement what if you want to pass some values that we'll see in the upcoming videos now in the earlier video we have talked about functions right so we have seen how do we define a function how do you call a function now we went with a very simple example where we created a function called as great and then in that we were printing hello world right but then we can call our function multiple times right and we have done that we are calling that function four times here so if you want to see the output this is what you got right you were able to run this and we were getting four outputs right in fact five we also got one here because we are printing it but at this point i just don't want to go for multiple calling i just want to go for one in this video let's try to understand how do you pass values to a function and how a function returns a value okay so what i mean by that is let's say when i call this greet i don't want it to do something i don't want it to print something i want it to return me the text example let's say i have a string here let's say a string and this string need to have let's say hello world okay of course you can type hello world here but don't you think we can use this hello world so what we can do is we can say hey greet do your work but also give me the text okay so i want to return this hello world to this str so we can call a function which will return you the value now when you want written the value you don't want to write console.log because you don't want to print it you want to return it how do you return it it's very simple you just have to use a simple keyword called as return so when you execute this function greet now it will return the statement or it will return the string called hello world so this will return hello world and it will be assigned to str so that's how you return the value right okay so can we have multiple return statement actually not because at the moment you have multiple return statement here so if i return hello so if you go with the flow of execution it will simply return the first other it will not even go for the second one right and that's why you can see it will also give you some yeah so it says it is unreachable code because i want to execute the function and the moment it sees this line it will directly jump out of the function okay that's how written works right so make sure that you write only one return statement yes if you can also have if else in that you can mention multiple returns i mean one return in if condition one in else condition that should work okay but then will it work let's try if you want to see the output we have to also print str right that's how we'll know that something is happening so let's after saving that let me create this screen and let's run this code and let's see what happens and you can see we got hello world so whatever data is getting returned here will be assigned to str and we are printing it example if i put some exclamation there now it will be updated with the exclamations right cool so that's how you can return a value how about passing a value so let's say instead of printing world here i want to say hello naveen or hello whatever value you pass example let's say here we have user and the data for the user is let's say naveen so when you have a user called naveen and i don't want to greet as hello world i want to say hello naveen so this will change based on what user you have so if the user is naveen it will print november users unless it chirag it will be hello chirag how will you modify it so basically you have to pass this user here right so how do you pass it very simple in the function itself in the round packets you say user this is that simple right so basically you are calling a function by passing a value which is a user here now since you are passing you have to also accept it right so here you have to accept the value now will you do it it's very simple you say user so you are passing a user and you are accepting a user cool and now you can simply remove this part and you can write user as simple as that right let's run this code and now i'm expecting it will print hello naveen wow we got hello user you know why we have talked about it before right we are putting this user in double quotes not a good idea so we can use a template literal here we can put a backtick and let's put this in dollar curry packet so it will be getting resolved to the value of the user which is in this case is naveen and we got it so you can see we've got hello novean so that's how you can pass a value and that's how you can return a value so using return statement you can return a value or you can pass a value it's not that you can only pass one value you can pass multiple values what if you want to pass two values you can give a comma and you can pass another value or you can pass 10 values 100 values depending on the requirement how much you want to pass so i hope that makes sense in this video we have talked about how a function returns a value and how a function accepts a value now there's one more thing you can actually write multiple statements here is not that a function will have only one statement we are doing that from the last two videos in fact this one and last one but you can have multiple statements here maybe you want to print something maybe you want to calculate something maybe you want to store it in data in database maybe you want to send a request to the server whatever you want okay and we will do that in the upcoming videos don't worry in this video we'll talk about function expression now see we have talked about functions we can create functions we can pass a value we can return a value and then you can specify the parameters as well and those things right but now let's treat function as an expression but before that we have to understand what is an expression see if you say you have a variable let's say the variable name is a and then when you assign a value now this value can be an operation as well so it can be 5 6 right now even before assigning the value of 5 plus 6 to a first it will evaluate 5 plus 6 which is 11 right so it will assign 11 to a so that means this 5 plus 6 is an expression right so expression simply means it will evaluate and it will be assigned to a particular variable cool if that makes sense okay this is the first thing you have to understand the second thing is uh if you remember before talking about functions we have talked about objects right now normally when you talk about different languages we discuss about functions and then we move towards object now what is different in javascript is javascript tree to functions as an object okay but that's tricky right if you remember the earlier sessions when we talked about data types we have primitive types and we have object types whichever type is not primitive is an object right and in premiere we have not seen function so function is an object type okay so javascript treat function as an object okay so that is awesome right let's try to understand what is function expression so now we know what is expression and we also know that function is an object right so what i will do now is i will just create a function but then this will not be a normal function let's say i have a function add and we are saying equal to now that's weird we are not creating a function like a function and then add so we are creating a variable here which is add and now a function will be assigned to this variable okay that's tricky how do we do that we create a function now this function will not have any name that means it's an anonymous function or we can specify the parameters maybe i can say num1 comma num2 right so basically we are creating a function which is anonymous which is taking two parameters and then this function is getting assigned to this variable add so this is called a function expression because this will be evaluated it will be assigned to add okay that's cool right but then we are not doing any stuff here so let's return uh num1 plus num2 of course you can write multiple lines of code there's no restriction that you should be having only one line of code but as of now just to keep it simple we are doing this okay so basically we got a function here and this function is getting assigned to this ad but the question arrives how will you call a function which doesn't have a name okay so this function don't have a name but this is assigned to add right so we can create add as a function now okay let me show you how so i can say add and i can pass two values i can say five plus six so basically we are calling a function add but then this will be calling this particular function right but that function also returns a value so let's say let result is equal to add 5 comma 6 okay so this is your function expression and now let's print the value of result and i hope it will work and it worked you see that so what is different again this is a anonymous function because it doesn't have a name and this function is assigned to a variable called as add and we can treat this as a function now okay in fact the awesome thing is we can say let sum is equal to add now what will happen is this variable will be assigned to sum now we can treat sum as a function so basically we can play with this uh thing here and let's try and it still works right uh so this is treating as an object so you can imagine this is a function as an object assigned to a variable and then we can play with that variable now okay but what happens if i try to print sum here now sum is not a function right it's a variable but then we are assigning a function to a variable so let's see what happens if you run this code you can say it says it's a function called add right so the add is getting assigned to sum so still it's another function ultimately cool now we are talking about functions right so we have seen how do we create a function how do we call a function how do a function return a value and then how can you pass a value okay now it's time to understand how do you work with variables oh we have talked about variables right but then when you talk about functions we have something more to talk about variables see we have a user here which is variable right and then we are passing that variable in this function which is great and then we are able to print it okay but what do you think is this variable user and this one are the same of course not they are different right because this is a variable which is created inside the grid function and this is the variable which is created outside so there are two different functions yes they have the same name that also means that if i change this variable name to u it perfectly works it's just that we have to print u okay so let me just try it out now and let's see what it does and if you're on this code you can see we are still getting the output cool so this is working right now what i want to understand is can i access this variable inside this function let's try so let's say this is you now this is different right so i will come back here and i will try to print the value of the user now let's see what happens will it give you any error it will print something the moment you're on this code you can see it is working you can see we got naveen which is working now you might be thinking hey this function is defined earlier and then we are creating this variable which is created out after this function definition is it still working there's a concept of hoisting which we'll discuss later as of now we'll ignore that let's imagine this is a first line okay because something is happening behind the scene which will be difficult to understand now later on we'll understand that perfectly as of now we got this variable on top and then still it will work the same way we have done earlier now what's the difference between this u and this user now this u is created inside this great function so we will call it as a local variable this user is created outside we will call as a global variable okay now local variable can be created inside this parenthesis or it can be created inside this block example i will say a new variable here which is num equal to 5. so as of now we know that we can use the outside variable inside the function so basically we can use the global variable inside a function but how about using the local variable now y is local because it is local to the function you can't use it outside let's try so first of all let's use that inside the function let's try to print num and let's see if that works and you can see we got the output right so we are able to print num but what if i want to use this num outside so here i will try to print them and let's see what happens in fact let me print a statement as well i will say num value is just to know that we are printing it outside and now let me just create run this there is an error it says num is not defined you know why because we have defined it inside the function that means it is a local variable this user here is a global variable so both this one uh the variable created inside the parenthesis they they also local variable and this one which is created inside the block is also a local variable and the variable created outside they are global variables so why it was working when we mentioned that after the function as well is because of the concept of hoisting which we'll see later okay cool what else we can talk here so let's see if we can pass multiple values as we can see in the grid can we add two numbers let's try that so what i will do is i'll just remove the entire path here and let me just add two values so i'll get a function and then this function will accept two values right we want to add two numbers so i will say num1 comma num2 so we got these two values here and then in this i just want to return the addition of num1 num2 so i will say num1 plus num2 semicolon a very simple function right and then i want to call this function and the way you can do that is by saying add and you can pass to values so let me pass 5 and 6. now this function will add two values right so we are doing that here it will also return the addition so here we have to accept it right so we'll say let uh result is equal to add 5 comma 6 and then i just want to print the value of results so i will say result will it work let's try so i'll just clear this screen and run and you can see we got 11. so this is working cool so we can basically pass values and it will accept it so in the earlier example we have taken only one variable this time we are going for two variables okay can we have three variables here of course you can so we can say num3 that perfectly works and we can pass the third value as well we can say five six and eight and here we just need to when you add this you have to also add num3 how can you miss that right let's try and even that works so basically if you can accept multiple parameters not just one so these are parameters these are arguments which you pass okay there is one more thing what if i am accepting three values but then i am passing only two values can we do that now what will happen so imagine this what will happen if i do this if i'm passing 2 i'm accepting 3. so if you're on this code you can see we got an error what is the error is it really executing let's try first of all let me just print the value of num1 and num2 and num3 let's see what happens so you can see when you're on this code you can see we got five that means it is accepting those values so it will try to match the method which is or the function which is add so it is working uh you can see we are passing five and six it will assign the value to the first two variables the third value will be undefined and when you add this with undefined you're getting a nand here so at least it is calling the function right so we know it will try to call the function but what if i want the output i don't want nand there right i don't want not a number so in this case you can assign some default values to the variables so i can say num3 is maybe one now when you assign num3 as one so if you don't pass the value for the num3 it will take one as the value so you can say just taking five six and one cool right but what if you pass the value so we are passing five six five in this case it will take five as a value so default value is applicable only when you are not passing it so in this video we have talked about how to create a default variable here we have talked about local variable and the global variable so we'll talk more about global local but then since to go ahead we need these concepts in this video we'll talk about arrow functions now in the earlier video we have talked about function expression right where you can create an anonymous function and you can assign to a variable now using arrow function you can simplify it more now in terms of the way it behaves but then the way it looks it will reduce the number of lines or number of words in the code so what we'll do is let's create another function here just to understand this and we'll comment this section here so in this another function let's call this function grid which we have worked with earlier and then i want to create a function expression so i'm creating a function as of now it is not accepting any value we don't want to and then i just want to print hello world and here we'll return one i don't want to write anything but just for the example we are going for this written one statement and how do we call it it's very simple we can simply say greet in fact greet will also return something right so i can simply print it here i can say great and i can call now why i'm printing it because it returned one so i just want to print one as well so it will call this function it will execute this statement it will print hello world and it will return one which we are accepting here and then we'll print it let's see if that works and when you're in this code you can see we got hello world and we got one now don't you think whenever you create a function like this don't you think this is something you can actually imagine see behind the scene every compiler or every interpreter helps you with the number of extra lines okay so this is not something which exactly get executed there are some more statements which will get executed which your compiler will add okay so this is something i don't want to write i want javascript to actually imagine this and add it by itself so i don't want to type function and i want this block to be a part of this function so this is a shortcut here so basically you don't need to mention that function name and you can simply surround brackets and you can mention that is a part of it i know it doesn't make any sense as of now because we just had a function which we removed it but then this is the type of syntax you will see very often now why this is important than the way we did earlier see there is two things here one we will be reducing this code more in the future but as of now we don't have any keyword in between see this is a behavior right function means behavior and then you are assigning this behavior to a variable why you need a function keyword in between it basically distracts you from your flow of logic right so that's why it says hey don't write function just mention that round back the javascript compiler understand hey that's your function there and this is a function body but now you will think hey what if you have certain variables and you are accepting some variables let's try that so i'll just undo this part and let's say we are passing some variables here let's say i'm passing naveen here as a parameter so this is the argument and i have to accept it right here so i will say user networks so i'm accepting a user and maybe i will use this user somewhere maybe i will use it here itself and i will say hello user so we are just doing this just to understand as a function right so here let me print this and let's see what happens and you can see we got hello naveen but what if now i don't mention this function here and i want to make this work so basically to make this work we have to use a round packet and after this you have to mention a arrow function so this is called arrow okay this is a function and this is your arrow here now let's see if that works and this works right so this is what arrow function looks like now to understand it more we will try to convert our logic here so now you try to guess what is what i will be doing here even before i type the code you can pause the video and try to do this in your mind okay what i'm what we are going to do okay so basically this is a function we don't want to add a function and this is the part of function so i can write a arrow here so we can say function parameters and an arrow and we are doing this okay so this is how error function looks like now the amazing thing is in arrow function if you have only one statement you don't need to mention cuddle packets plus you don't even have to mention return statement if that's the only statement you have okay and then you can write everything in one line i know it was tricky so pause the video okay first of all go back and watch it once again or maybe i will do it once again for you so let me just show you what what i did so basically this is a function right we don't want to add function because we want to use either function and since we are using error function we'll be using a arrow symbol here now whenever you have a arrow function which only has one statement if you can see here we have two statements so we don't have a choice here we only have one statement and when you have one statement you don't need to put curly brackets and when you have only one statement and that one statement is returned you don't even have to mention return and you can write everything in one line i know you'd trust me so let me just remove this part and look at the code now if we don't even need some here it was unnecessary let me just go with add and let me print result okay let's run this code and you can see it works you you can see we got 11. so this is an arrow function that's the benefit of it so basically we tried to convert an anonymous function into an either function with the help of this arrow keyword so yeah that's it from this video in fact i have a assignment question for you so the assignment question is in fact we'll let me get a video separate for that and the question is you need to take two values this is 5 and 6. now you need to add these two values the only condition is if you get a negative value let's say you got minus 6 here still you have to add 5 and 6 okay not negative six that's your question or maybe you you can simply skip it that's what we want so add it only when you get a positive numbers don't add it when you get negative numbers you can simply print a message or something so that's it from uh this video uh we'll try to solve that in the in the next video i hope you enjoyed this video writing in the comment section and in this video let's see how do you add a function to an object see we have talked about objects we have talked about functions and then we know the basics of it right see every object will have two things we have talked only about one thing there uh so a object will have the data an object will also have the behavior see as an object right as a human or alien we are the object right so we have two things we know something and we do something we know something because of the data which you which is stored here uh the property values and the names of your friends the favorite movie the concepts which you learn in javascript everything is stored here and the way you talk the way you walk the way you dance is all your behavior right and the way you behave is sometimes depend upon the way the things you know right so these two things are related to each other what you know and what you do so what you know is with the help of variables and what you do is with the help of functions so an object will also have the function right but then we don't call them as a function we call them as a method it's ultimately a function but technically we call it as a method okay so how do we do that so first of all let me remove the code and in this code basically we will be creating an object which we have done earlier the same object or let's go for different object this time let's go for laptop yes we have worked with laptop but it was a part of the object right but this time let's create a laptop object outside and the way you do is very simple you say let laptop and then equal to you you want to get an object here right so we'll use uh cut the packet open close there are different ways of creating object we have seen one this is the second way uh so what we'll do now is we got this object right which is laptop uh let laptop and here let me create some properties okay what are the properties we need so let's go with cpu so the value for cpu let's say is i9 and then we'll also mention the ram so in this case let's say ram is 16 gb and then what is you need maybe you or we can also go for the brand name we'll say hp so basically we got an object right and i want to add a function here now as i mentioned before we will call it as a method but ultimately that's a function right now how do we do it now first of all even the method will be our property right so let's mention the property name here so maybe i want write a function which will print hello world as of now let's go with that so i want to get a function inside this laptop which will print hello world so the way you can do that is by saying get value or maybe we can also say greet just to keep it simple and this greet will have a function right so we have to give a color not equal to we have to give a colon the same thing we have done for the function expression right we have to use a variable name and then we give equal to then we mentioned the function right here also we have to mention the property then we'll say function and then we can perform the operation here what i want to do here is i want to say return or maybe i can just print hello world okay so we got a function here right and then we have some issue here okay and the issue is we forgot to put a comma so as i mentioned before you know comma is very important here so we can also put a comma at the end which will not give you an error but we if we add the next property it will get resolved automatically right okay anyway so once you've got this how do we execute this it's very simple the way you do it for the function we just have to say greet and we have to give the round bracket that's it but what do you think will it work let's try so i'll go back here and let me execute this i will say node space app.js so when you say enter you can see we got an error now why there's an error here it's because we are trying to call this grid function and it's not defined because it is inside this laptop object right so basically this is a method here not a simple function is a method which belongs to an object so that object is a laptop here so we have to say laptop.greet okay let me just run this code once again and let's see what happens and you can see we got hello world so basically a function or a method belongs to the object here which is laptop and if you want to call it you have to use the object name now of course with this function you can pass some parameters you can return some values and we'll see that later but as of now we are trying to create a function or a method inside the object and you can get multiple functions here so you can say greet you can say get me the values or you can say calculate the speed of a machine or do the benchmark everything just add multiple functions and in this video we'll talk about this keyword i know that was pun intended the thing is this is actually a very simple concept but then it takes some time for your brain to you know get used to it so we'll divide this in two parts so we have two videos actually the first video will talk about what is this keyword and the second video will talk about why it is called this keyword okay then it will make much more sense so if you remember the last example where we have created this object so if you can see we have this object which is laptop and then this laptop has some properties right now this property i mean we have let's say cpu ram and brand and then we created a method as well so what is a method imagine a object having a property but it adds a behavior and we can add the behavior with the above function and that behavior for an object is called a method okay this is a method here and then we are trying to call that method right so this is the simple thing we have done and just to see what happens when you when you run this code let's try so beyond this code and you can see we got hello world so i don't want to print hello world here what i want to do is i want to print the values of the object right so the value we have here is let's say i'm the cpu ram and brand i want to print those things and let's say not even with a function called greet or method called greet i will say get config so let's say if i have this method which is get config and every time i call get config should print the configuration so of course the method name here should also be get config not greet now what do you think will it work let's try so what i will do now is i will just remove this part from here and let me replace this with the help of the variables of course we want to print the variables right oh the properties so let me print cpu and let's see what happens if i try to print a variable cpu and let's see what happens and we got an error it says the cpu is not defined i know that's weird right cpu is defined there it's here but it is still not able to find it okay maybe there's a problem with the variable outside the functions or method right let's create a variable inside it so i will say let's store this that's a variable i want to create and let's say i will say this is hundred and then here let me print storage let's see if we can actually use a variable inside a function i don't know if we have done that before but let's try this time so let's run this code and you can see we got hundred so that means there is no issue of using a variable or printing a variable inside a function it is able to fetch this variable which is storage but not cpu you know why first of all cpu is not a simple variable it's a property of an object right so if you want to access that here we need to do something else we have to let's again let's type cpu but then if you really understand this cpu is not a part of the function here right it's a part of the object it's an object property in that case you have to use the object name so i will say hey this cpu belongs to an object and the object name is laptop that's simple so the object name which is a laptop dot cpu let's see if that works and that works you can see that every time you want to use this property you have to use a laptop name okay this makes sense right but the problem is see you can have multiple objects right let's say this can be laptop one right and then you can have laptop two then you can have laptop three there are different ways of creating object this is one one way of creating an object you can create an object with the help of a class where you don't actually define an object like this you don't say laptop one and then the properties then laptop two then the properties of course right laptop will have the same properties every laptop will have a cpu they will have a ram it will have some brand and you will have for some function yeah the value will change i mean they might have a different cpu they might have different uh ram they might have different brand but the point here is when you try to use an object to call a property using the laptop or the object name is not something you should go for what is preferred is instead of using the object name we can set this now what is this so this means the object so whatever the current object you are referring to you might have multiple objects you might have laptop one laptop two in fact let me just try that so i will select a one and then here i will let me just say laptop one and it should work i guess oh there's an error uh the other is because of this laptop here then it should be laptop one as well and let me run this code and you can see we got i9 what if i have one more let me just create it on top so that it will not be in our focus and let me just name this as laptop 2 and here also i will say laptop 2. but if i go down and if i say this cpu i want it from laptop 1 and you can see we are talking about the actual object we are using the name of the object and it should work there should not be an issue but there is one more thing what if you say this is i7 okay and here you are coming back and say this is laptop 2. so basically you wanted to refer to the same object but since you have a choice you went for another name which is laptop two and let's see what happens and you can see we got i7 we don't want that right we want this cpu to be the value coming from the same object so it's always better instead of using the object name use this keyword so this represents the current object okay what is current object so if you are in this method who is calling this method the laptop one is calling this method so based on which method or which object is calling that method it will refer this to that particular object what i'm saying is if you call this with laptop2 now this will refer to laptop2 let's try and you can see we got i7 it is happening because of this method here so of course when you call laptop2.getconfig you will get this method here so basically always use this keyword so what is this keyword this represents the current object so if you want to access any of the property of an object it's always better to use this keyword but why exactly this keyword why not something else that we'll see in the next video in the last video we have talked about what is this keyword in this video we'll understand why it is called this keyword and that's important you know when you understand the concept so in the last video we have seen why do we use this and then what is this keyword in this video when we understand the meaning of it you know next time when you want to use it it will be easier for you to understand hey this is where you want to use this keyword right so let's understand that so what we will do here is if you can see we have two objects here right we have laptop one and laptop two and both these laptops has some different properties i mean of course the property names are same the values are different uh so this laptop too has cpu ram and brand same goes for laptop one but what is different is the values right uh in fact for the laptop tool let me change the brand from hp to let's say apple so we got this machine apple here and then we got hp machine on this side and then i want to compare the speed of it now technically it doesn't make sense to compare two brands if they're using two different os example on apple you'll be using mac os on hp you'll be using either ubuntu or a linux based os or a windows os right so what we will do here is even let's do a comparison but let's not compare the brand and ram i want to majorly compare the cpu here so irrespective of the os let's only compare with these cpus i know the recent apple machines are coming with m1 chip but let's go with the older one which is i7 so here i want to compare this to how do you compare it it's very simple actually uh you can come back here and you can write if condition here right so you can say if the laptop one cpu that's how you compare right because if you want to access uh the cpu property you need to use the object name as well you can't simply say cpu and compare which oh watch that okay so i just said backspace and so i will say cpu uh compared with the other cpu right so i'm comparing two different cpus of two different laptops right so this is of laptop one laptop two and you can see it is giving you as well i mean it doesn't know what that cpu is so we have to mention hey this is of laptop one so this is laptop one cpu and this is the laptop two cpu so that's how you refer it so we say laptop one is greater than laptop two cpu in that case uh we can print uh the laptop one is a major one in fact we can just print it we can return the value or we can print it but here uh where do we return it we are not calling a function right so let's print it here itself so it's a log and here i will print the one so we are saying hey laptop one is greater or the it is a faster machine uh else will print laptop two so this is what we want to print right so here some settings has been changed in this machine the keyboard is actually running very fast i have to reduce the speed of it okay so we are printing laptop one here we are printing laptop two here depending upon which one is faster right let's run this code and let's see what happens and you can see it is printing i7 of course that's the condition we are doing here a laptop one is faster but what if this is uh okay i know we don't have i11 but let's make this i5 for time being just to see if this really works and yes you can see this time it is printing i7 so earlier it was printing i9 this time it is printing i7 that means something is working right uh i will go back to i 9 here so that we can do some more comparisons okay so we can do that here itself right what if we don't want to do this here we have talked about functions right normally when you have a task it's always better to have a separate function for a particular task right so this should be a part of a function we can do that actually so let me just take this and let me cut this part and let's go on top let's paste it here and let me put this part into a function now do we do that it's very simple we have done that before so i can get a function and i can call this function as get the fastest machine or they get the fast laptop of course you can name anything it doesn't matter actually so we can say get fast laptop and then this will return me a fast machine right so basically this function will accept two laptops or maybe you can have three laptops or five laptops at this time i'm going for two laptops so this function will accept two laptops here and then it will compare these two laptops based on the cpu of course you can add some more parameters to it but if there are two cpus which are same in that case you will check for let's say graphic card performance if that is same maybe you can go for the ram storage or the ram capacity maybe you can go for the uh some other aspect of the frequency of the ram and all those things but at this point just took a bit simple we are going for cpu comparisons of course you can do that as assignment uh you can check hey what if that there are two machines or two laptops with the same cpu what next you have to check maybe you can use a and operation there or maybe you can have a nested if condition again that's your choice i would love to see your quote in the comment section uh but as of now let me pass those two laptops in fact the operation is this we are going to do which we have already done this is what you have to compare right but before comparing you have to pass those two laptops i will say laptop one and this is laptop two so basically if you want to compare you have to pass those two parameters so of course if someone wants to call the get fast laptop so when you call get a fast laptop you need to actually pass those two values uh let's do that here quickly so we'll pass laptop one and we will say laptop two so basically we are passing two objects here right and what do you think will it work let's try so i will just run this code and of course you will see the same output because here we are doing the same thing right so you are passing to two laptops and based on which is faster you're printing it but what if you want return it in fact you can also do that but as of now let's not do this okay let's only print it so this is cool right so here what we are doing is as a third party object because of course someone is owning this method here right this function so someone is actually calling this function and comparing these two laptops what if a laptop is comparing itself to the laptop is it really possible what i want to achieve here is this laptop here laptop one laptop one should be able to compare itself with the other laptop maybe i can say compare right so laptop one is comparing itself to laptop two okay and it should print whichever laptop is faster so if laptop one is faster it will print or maybe it should print true or false uh or you should print uh yes you are the greatest or you should print oh you lost it okay something like that so laptop one one object is comparing itself to other object now what's the difference between this method here and this one is this is a third-party function comparing these two objects right so laptop one and laptop two have no idea about get first laptop it's some other function comparing two laptops it's something like you have two phones in front of you and you are comparing them right so you are you're the third party those are the two phones but this time a phone is comparing itself with other phone so this is changing the perspective of course both should work both are comparing two objects here we are comparing laptop one with laptop two but will it work of course we don't have this function compile right if you run this code i don't know what will happen so do we have that code oh no we have to actually define this compile method right so we don't have compiled inside laptop one so what we can do here is we can go back to laptop one which is here and in this i can add that property so we have to add extra property here which is actually a method so i will say hey i want a compared feature as well and we can call this as a function right so this is a function right which will make it as a method so let's define this but there is a issue if you can see here you are calling a compare method that's awesome so laptop one is calling compare but we are passing a parameter okay so that means we have to accept that parameter here now what should i call it maybe i can call it the laptop yes we can go for laptop one we can go for laptop two we can change the terminology even if you say this is laptop one that's completely fine because the variables are different because they are from different scope as of now i will say laptop and here let me compare these two laptops or maybe you can go for laptop two just to keep it easy right so we got laptop two because we are passing laptop two there but now questionnaire is how will you compare because to compare two laptops we need two laptops so we are only passing one here uh so even if you want to code the same code which we have done here i can simply copy it you know what uh since we only have one statement you don't actually need curly brackets just to reduce the number of lines it's very important to understand right okay so if even if i copy the exact code so let me just copy this and paste it inside the compile because ultimately doing the same thing right it is comparing two laptops but the problem is we have laptop two we don't have laptop one so that's the challenge we have to address here how do you solve this issue we don't have laptop one right but don't you think you are calling this compare method with the help of laptop one so somewhere laptop one is executing this method and every time object executing the method it's there it's just that you can't see it it's john cena it's there you can't see it right so how do we use it how do we use that invisible object which is calling compare but here you can't see it the thing is whenever you have an object executing the method or anything in that particular object we can refer that with a awesome keyword called this right so this object is comparing itself with the other objects instead of using the laptop to name we can say other so this is comparing itself with the other one of course other is not a keyword in javascript but we can use it so what we are doing is we are comparing itself with the other object so who is calling compare laptop one and it is passing which object laptop two so the current object which is calling compare is called this keyword can be used with the help of this and the parsed parameter can be used as an object itself so we are comparing this dot cpu with the other cpu example like if you are comparing your marks with someone else you will say okay my marks is greater than the other person marks so we say my right that's this keyword here okay i hope that makes sense it's actually tricky to actually understand this in one go right we forgot to put a comma here so this is how you define it and let's see if that works now let me just remove this get fast laptop in fact let me remove it completely so that we can have a small code which you can compare okay uh so let's see what happens when you're on this code and you can see we got i nine uh so i9 has all this properties which is compared and get config as well as the methods so this is how you compare two objects but the definition itself inside the laptop one okay that's why we said this keyword because it represents the current object now the other issue we have is can we do reverse can we compare laptop two with laptop one let's try let's see what happens and when you do that it will give you error you know why because the compare method only belongs to laptop one not laptop two it doesn't have that method so maybe we have to write this method there as well but then don't you think if you have ten objects of the same class or ten objects of the same type of laptop we should only define it once so that's one issue we will solve later as of now we are only focusing on this keyword in the earlier video we have talked about objects and then we have also talked about this keyword now in this video let's try to understand the different way of creating an object see nothing wrong with the way we did earlier right this is a basic way of creating an object in javascript what we do is we name the object and then of course we have to assign the values so these values actually are a key and a value pair right so we have a key and a value here okay so this makes sense and then of course depending upon the requirement you can have different kind of objects here we are going for laptop objects you can have let's say alien object or account object a user object depend upon requirement you can change it but looking at this example here don't you think this laptop 2 and laptop 1 are the same kind of objects so basically they share the same design right so basically we can call it as a blueprint now if you're coming from the world of java or python you know what i'm talking about what i'm talking about class as an object but here we'll not talk about class as of now let's talk about the blueprint what i want here is i want to get multiple objects of course so what i want to do now here is i want to create multiple objects but of same type so laptop one laptop two maybe i want to get laptop three laptop four laptop five every time i don't want to mention the properties this has to be common right so what we can do for this is let me remove the entire part here so what we can do here is we can create a constructor function i know that sounds weird but what exactly we need here so let's say i want uh maybe with this time we can go for the alien we have talked about laptop before let's go for the alien so let's say i want alien1 so i can simply say let alien1 now this should be able to give me an alien object and of course i want to assign some values as well uh let's say i just want to assign the name and the technology so in this case i can just say hey i want an object and i want to assign some values maybe i can do this i can say i have a function let's say the function name is alien itself and this function will accept two values the first one is name and the second one is let's say technology and this time let's go for javascript this is how i want to get it done uh this may not be the exact way it has to be done but something like this i want an in an object so maybe there should be a function which will take all the parameters whatever parameters you have and it will assign the values to it the advantage would be if i create multiple objects let's say alien2 alien 3 alien 4 i just have to call this function and pass the values okay this sounds good right but how will you do it so basically the way you can do that is with the help of constructor function so what we can do here is we can create a function as i mentioned is a function so we'll say function and then we'll name a function of course we can go for the same name but what makes sense how do you differentiate between a normal function and a constructor function so it's recommended you go with a capital letter function name so that it will define it as a constructor and here you can accept those two values so what i'm passing is a name and i'm passing a technology so i'm calling a function and then we are assigned the values here okay but there's one more thing here which you have to remember so the first thing is the first letter of the name make sure it is capital that's how you differentiate between normal function and a constructor the second one is every time you create a new object example we got agent one here maybe i want to get alien two or eight and three just by calling a function it will not work you have to use another keyword called new now what this new keyword does is it will create a new object for you so every time you use this it will create a new object uh the advantage would be if you make changes in one object it will not reflect another object and that's what we want right we want multiple instance of the object uh so let's say if you are paying for five iphones you want five different iphones not one iphone representing for all five okay so that's one thing next is every time you do this it will also give you a this keyword remember when you create an object you get at this keyword and that's how you can assign the values as of now this object doesn't have any properties here so whatever you're passing is actually normal local variables right so in this case what we can do is we can say this dot name that's how you refer to the object variable and you can assign to a name here and we can give a semicolon the next one is this dot tech and we can assign it the tech variable and that's it and by default it will return the object of the alien so what it will receive here is the object of the alien and using this you can create multiple objects now what it returns is you can't see this code but it says return this okay so this is something again john cena you can't see this it's there you don't have to mention that anyway so that's how you create a constructor function in javascript uh but will it work let's try we have not ran this code right let's do that so i will just say log i just want to print the agent one and let's see what happens and you can see we got the output so it's alien object and it has two values what if i create another object here so i can just copy this and paste and we'll name this as alien2 and this is let's say kiran and the technology is let's say java then this code and you can see we got navin because we are printing navin and let me print the alien2 here and if you run this code we call it kiran and java now these two objects are different let me prove it what i will do here is i will just change the value from alien1 and it will not reflect all on alien2 so i will just change the technology from of course there are two different objects we are able to assign the values as well but let's say the technology here is blockchain and now you find this code you can see we got kiran java so this is not changing for kitchen of course if i print the alien1 it will be assigning that new value which is the blockchain that's how you can create a constructor function this is a very special type of function which is a constructor because you're using a new keyword here okay uh this makes sense right but can we add a method in fact we have talked about this in the earlier videos right we can have methods to the object but then that was a normal object and we were able to assign it and then we have seen one problem if you create multiple objects and what if you want to share a common method we can achieve that with the help of constructor function so let me create a new method here let's say work so as an alien we write code right so we work as let me create a function here and we'll say what this function will do as of now it will just print something and i will say solving bugs from dwellers okay that's what we do right we write less code and we debug more that's the thing so once you've got this you can simply use this any any of this object here and you can call the thing so you can say alien dot work and you can simply call the function it should work let's try and you can see this it has that function which is a work and it says solving bugs from tools so this is how you can create a constructor function and you can also add a method to it anything else yeah there is one more thing what if you want to return something else uh yes you can return the object but what happened you try to return a value that's a 7. of course right by default it says return this but if you try to do something else let's see what happens and if you can see it doesn't have any impact so basically it just skips this line if the return does not return object it will skip it okay and so if you get stuck somewhere remember this in this video we'll talk about arrays see in the earlier video we have talked about objects right and then before that we have also talked about primitive types and then we have also discussed that whatever data is not a type of primitive it will be an object now this can be the object which we created then we have functions which are objects and then we have areas which are objects but then when you have an object why you need another concept like arrays so if you look at the object we have a key and a value right example when you talked about aliens or laptop it had a key and a value pair example for laptop it can be cpu which is your key and then i9 i7 those are your values maybe you can go with another property which is let's say ram which is your key and then let's say 16 gb is the value but in this case let's say you want to have multiple values and you don't need to have a key so let's say you have a lot of values and then you don't want to assign a key to it maybe you want to work with the index values can we do it now if you're coming from other languages like cc plus with java most of the languages has this concept but if you're new don't worry whenever you have a collection of values which you don't want to assign a key to it maybe you can use numbers to represent them we can use a concept like arrays how do we create an array here so let's say i want to create a array of values so what we can do is to create an array we can say new edit this is one of the syntax which we can follow so we can select values equal to new array and now you can guess this is a constructor function right because we are using a new keyword so basically we are trying to create an object okay there is one way of creating an array the other way is you can directly assign a value here itself maybe you can give a square bracket and you can assign the values okay so this works on the same with the other method work so we'll go with that only but again you can replace that with the new array keyboard here so let's use the empty square package this is another way of getting an array now this is your empty array so what we have done earlier with the help of new array is same as this one both will give you an empty array okay so when you empty it will not have any values right how do we check that so it's very simple you can just print the values to verify right so we can simply come back here and we can say values and when you run this code of course it will not print any values because it's playing so yes it is printing it's an array but we don't have any values now how do we assign a value of course there are multiple ways of doing it so what if you know the values already so in that case let's say the values are five seven and eight that's how you assign the value so you can say five comma seven comma eight and you've got an array with these three values of course there's no compulsion that they should only have numbers you can also have string or you can have other objects you can have a function your choice let's print this and you can see we got an array with three values that sounds cool right okay can we just print the length so maybe i want to know the length of this array so that's simple you can simply come back here and say length so values.length and it will print the length of the array and the length is three okay so you have three values in this array quite simple right so this is how you create an array the same thing you can do with new ad it's just that new array will give you empty array and you can assign the values later but how how do we assign the values later so let's say i want empty adder here and i want to assign values in that case you can use certain functions maybe you can use a function like push or you can say methods because this thing actually belongs to an object right so i can say values dot i can say a method which is push now what this push will do is whatever value you will assign here let's say five or this value of five will be assigned in this array okay so when you say push it will add the element let's see if that works uh let me just remove this length i want to print the actual values and let's see what happens ah so you can see we got only one value now which is five so initially it was an empty array but then you said hey i want to add one value it's doing that what if you repeat this what if you just copy this and add another value and that's a seven and what will happen now so it will have five and seven and likewise if you add some more values it will add that as well okay uh so that's how you can push the values but again i will see this later let me just go with the predefined values here which is five seven and eight okay so when you print the values it's quite simple right it will simply print all the values but what if you want to fetch one particular value let's say i want to print the first value or the second value third value how do we do that now we have seen this in objects right in object what we do is if you want to fetch a particular value you ask for the key right so you mentioned hey we have this value i want to fetch that with the help of a key but in this case we don't have a key right we all have values here but then as i mentioned before maybe you can fetch this with the help of numbers something like index numbers so this 5 will have a number this 7 will have some index number 8 will have some index number example you know when you create a table you have a column which is the first column right which is serial numbers right uh example the number of the line number here so these are the index numbers you can imagine uh but for this value of phi what should be the index value so what i will do is uh first of all how do you measure index value uh the same way you whenever you mentioned at a it always represent with the help of a square bracket so you are saying hey i want to fetch values and i don't want to fetch all the values i want to fetch the values with the index number so i can say hey i want to fetch the first one so i will say one and let's see what happens so when you mention one and let's run this code oh oh we got seven we wanted five but we got seven now what went wrong here is uh in computers basically whenever you talk about index values it always starts with zero so this five here it should be zero the index number is zero here okay so we should start with zero so when you say five is zero then seven will be one and eight as is at location two okay let's run this and you can see we got five okay uh how about if i say one uh it's quite simple now you know it should be seven we have done that before and then when you say two it will print eight let's try two as well and let's run this code and you can see we got eight but what will happen if i go for three because we have only three values and when you say i want to fetch the element at index number three that means we are trying to fetch the fourth value we don't have a fourth value here what would happen you will get undefined so luckily you don't get errors in fact i'm not a big fan of undefined i believe we should get either that there's no element outside three or maybe you can you should get something like in java you get added index autobound but here we don't have that you know it's printing undefined uh but that you have to remember this whenever you have the add a if you want to fetch a value maybe you can first check what's the length of the array and then you can fetch the value yeah so that's how you can create an array and that's how you can fetch a particular value how do we add elements with the help of index value that will say in the next video so in the last video we have talked about areas right and then we created an array we were able to fetch the values with the help of index values but then we only worked with the numbers right what if you have different types of data what if you have string what if you have different objects and if you have different functions can we add them here now there are two aspects here one the kind of data you're working with and second can we mix that data example let's say i can say all names here so i want to create a array of names and i can have different names here is it possible of course right when you can have numbers can we have names of string type let's try so let's say the first name is naveen and the only thing you have to remember is whenever you work with string you have to enclose that in single quote and it should work so i can say naveen kiran and then we can say chirag okay so these are the values you have and then there's no limitation that you should only have three values i don't know why i'm entering only three values but we can have uh thousands of values no not an issue so maybe navigating three values are here and if you want to print all the names i can simply print all the names there's no harm in that and you can see we got all the values and of course you can specify the index value to fetch one particular value there and that should work now what i want to do is i just want to add another value here maybe i just want to add one more name can we do that so let's try now there are two options here one you can add the value here itself we can say comma and then we can add a name here that's one way the other way is what if you have this array created and then you want to add a value later in this case you can say names dot so in this case you can say name start push we have seen that before right that's one way another way is you can use the index value so we have naveen kiran those are the three values you have so this is index 0 this is index 1 this is index 2 okay so now we can add a value at index 3 and that value here is let's say omni okay so basically in total we have 4 values now let me try that and you can see we got four values so this is how you can add a value of course you can also use push uh that's a method and this is another way you can do it so you can print all the values so you can print one specific value so this is one thing the other thing is can we have different type of data uh let's say i want to have a data like maybe i can just say this is data let me just remove this let's say the first one is a string the second one i want to insert is a number can we do that let's say i want to add five no issue when you create an array there's no compulsion that data should be of single type it can be string numbers you can also have object let's say i want to get an object here can we do that yes i can say an object here and this object will have some values let's say technology is javascript that's the object we have the another thing we can have is a function we can also do that so i can say a function here so i'm creating a function you know why we can add a function is because function is object remember so in this case i'm creating a function which will only print console log and let's say hello world i know you can't see that in one line let me just take it on the next line so you can see we got a data which is an array of different type we have a string a number uh object itself and of course this object can have multiple properties just to keep it simple i went with only one and then we have a function here what happens if we print this or we are not printing names we are printing data which is an array here and you can see we got everything we got a name string we got five we got object and we got a function can we call that function that's a question right so function is at element what so i'm printing something right so i have to do that here maybe i will do that after printing it so i will say data and then i have to fetch that function which is at element three i mean index number four so in element three and once you get that you have to call it right so the way you can call that just with the help of this round package and it worked you can see that we got hello world so this is working call you can also get this object and you can assign to some other variable you can try that on your own and that's it that's how you can insert the values in the array and that's how you can create an array of mixed type that's it from this video in the next video we'll see some more methods like we have seen push i will talk about shift pop unshift and those things okay so in the earlier video we have talked about we can have different types of data in an area right there's no restrictions here in this video let's try to use some methods of an array one of it we have already seen which is push let's try to explore the other ones so what i will do now is let me remove this part we don't need that and let's simplify this so that we can understand the methods so let's go with the normal values which is this so let's say we have 5 7 8 and 9 and here i just want to use certain methods and then we have already seen one which is push but then what happens with when you do push so let me also print because push returns something right so what i will do is of course i will push the value and let's say i want to push a value which is two but i want to understand what happens when you push and what it returns so let's run this code and let's see what happens and here and you can see we got two things we got five and we got the value at the end now why we got two at the end is because we are saying push now what push will do is it will add a element at the end of the array so basically it appends right so that's what we can use so what is this five here so this five is actually the length of the array so after five seven eight nine and then it is adding two so it is your five right so that's the length the other method we have here is pop let's try pop and let's see what happens when you do pop so we'll just go back here and log now if you know the concept of stack which is last in first out whichever element you add last will be the first to remove okay so we'll say data dot i will say pop now if you don't know about stack go through it just google's what is stack and you'll understand that so what we are doing is we are pushing the value which is two right so two is going at the end what will happen when you say pop so on this code and you can see it says two now what is this 2 here so it will remove the last element so whichever element you added at the end will be the first to remove okay let's say if i don't say push here if i just say pop it will remove 9 so that's the last element right so it always removes the last element so it's remove five it will also print which element it removes so it will remove nine and it will also print which element it is removed which is nine in this case so we have talked about push we have talked about pop we have some two methods in fact three methods to talk about so let me remove this one so the other method we have is shift so let me say data dot shift and let me shift now what happens when you say shift so when you say pop it removes the data from the last right so shift will remove the data from the first basically it shifts all the values okay so all this value which is 789 will be pushed to the left hand side so basically it will throw five out of the array and when you run this code then you can see it was removing five so shift will push all the elements on the left hand side and it will remove five okay so that shift right so the difference between pop and shift is pop remove from the end and shift will remove from the start okay so when you have pop you also have push right so when you have shift you should have something called unshift that's right so using unshift you can actually add a element so let's say i want to add two now where do you think these two will be added so i want to know in the comments section when i do this you tell me where this two will be added let's try now so i hope you added your answer in the comment section let's try and you can see just printing two at the start of course right when you say shift it removes the value from the beginning so when you say unshift it will add a new value to the beginning now which is better the only problem is when you say unshift it moves all the element on the right hand side right so it is changing the index number so it's a lengthy process compared to push because push will add at the end so it's better to use pop and push it will not change the index number of the all the elements but yes you can also use shift and shift cool so these are the methods we can use here there's one more which is splice what we can do here is let's remove this and let's use splice so what it will do is you can mention the index numbers what if you want to remove the element from between okay let's say i don't want to remove from the first or last somewhere in between let me just add some more values just to understand so let's say i want to remove from eight okay so eight index value is three oh not three it's two right uh so let's only say two and let's see what happens okay it's breaking into two parts because it is removing everything oh i said dot yes okay so when i say two it will just remove the elements from two so it will remove all the elements here so basically we're just breaking down the only thing remaining is five and seven you can see that okay but then what if you want to remove only one element in that case you can give a comma and you can say one okay so in this case it will remove only one element which is eight right so we have five seven nine and four what it is removing is only one value okay but what if you say two so it will from index number two it will remove two elements which is eight and nine so the value you will be getting is five seven and four and that's right so you can you can see we got five seven four so the first value here is index value the next one is the number of values you want to remove so when you remove two elements you can actually add some extra elements here so we can give a comma so you can see this is a syntax here so we got a start so the index value then the delete count how many numbers you want to delete and you can also add some values now this is the area again we'll talk about it so you can have multiple values here maybe you can add one value so i want to add let's say 11. so it will add 11 by removing eight and nine so you can see it is removing eight to nine it is adding 11. you can add some more values your choice maybe you are adding 15 as well so it will add 11 and 15 in between so you can see we got 11 and 15 in between so yeah that's how you can play around and that's important to look at the documentations in fact if i say data.push you can actually see how that method works so it will give the definition it can accept multiple values yeah it actually accept multiple values forgot to mention that so instead of pushing one value you can push multiple values try that out okay cool so that's how we can use all these methods in the next video we'll talk about how do we work with loops what if i want to print all the values in the individual way that was in the next video so i hope you are enjoying this let me in the comments so in the earlier video we have talked about some methods in arrays right in this video let's try to print all the values but not all the values at once maybe i want to print one by one and then we know right if you want to print all the values if we have done that in objects as well if you want to print all the properties of an object we have used a special loop which is a for in loop but in for areas we have a different type of loop of course it's a for loop but it is called for off loop okay so before that first we need a array which will have a lot of values so let me create an array of nums and then let me create a very big addition now we have an empty area i know but i want to create a big adder here how do we do that of course you can push values or you can assign the values here but what will happen if i want to have an array of hundred values the first value let's say i want it to be five the last value i want it to be let's say nine and all the values in between i want something else so let's try let's see what happens i want to assign the first value so index number zero which is the first element and let's assign five and the last index which will be 99 right because that's how it works and then let's assign let's say nine so we got a array with only two values but then we have assigned the value to the first element and the hundred element here let's see what happens if you print this so what is your guess i want to know what is your guess in the comment section i hope you have guessed it let's run this okay so you can see it says we got five and then we got nine at the end in between we have 98 items which are empty okay it's not like it you're getting zeros but you got empty items you might be thinking why not zeros is because know that we have mentioned that we want to get integer values can be of any type right it can be string or it can be an object so that's why it says empty elements but that's fine with me i know we have total length of hundred let's verify that once so that before running let's do that and you can see the length here is hundred so at least one thing is sure we got an area of hundred elements now i want to print all the elements one by one so how do we do that so for that we have a special loop and that is called for off loop now how that works so basically what we want is we want a for loop to look at each element and fetch the value and give it to us and then we can do whatever we want we can print the value or we can perform some operation maybe you want to store the data in a database or maybe you want to send that data to the client i mean whatever you want to the important thing is i want to fetch that data now when you fetch that data where you will store it so let's say i will store that in n now n is a variable which will hold one value to time so i will say let n of nums so we have to use off here okay so what will happen now is from nums which has 100 values one value will be assigned to n at a time and with that value you can do whatever you want in this case i just want to print it so you can store that data in a database you can store that data in a file or you can send it to a client that's your choice here let me run this code and you can see it is printing nine and all the undefined in between because that is not assigned and you can see we got five so five at the start and then we got all the values so this is how you can use a for off loop but if you remember we have also seen for the in loop for the objects now for in what it does is it will fetch the key of the property and with that key you can fetch the value in this case if you want to use 14 so what i will do is i will just create another loop here so let's say four and then this four will be a fourth in loop can we use 14 here the answer is yes actually you know why because this is an object and it actually has a key also which is invisible which is actually the index value right so this five is index zero this nine is at index nine nine so though you can imagine that's that as a key right so if you can fetch the key so i can say let key in num so what this key will have is zero one two three four let me prove that so what i will do is i will just print or let me just print key here so let's not print these things let me comment that part and let me only print key now so you can say just printing 0 and 99 so what is this key here so this is the first element this is the last element all the keys in between are undefined so it is not printing it but you got the key right so you can use in as well but with this key if you want to fetch the value it's very simple you can say nums off that key and now you will get it and you can see we got five and nine cool so you can use fad off or you can use foreign specifically for arrays for off makes much more sense because you don't have to work with a key and then fetch the value you can simply fetch the value and that's how it works so i hope you got the idea how do we use this for loops and how can you fetch all the values in the earlier video we have talked about arrays we have seen how do we iterate those elements and then it was quite fun right but now it's time to understand how do we destructure and array now i know that sounds weird right because in the earlier videos we were creating arrays but now i'm saying these structuring so let's understand what do i mean by destructuring an array so here what we will do is let's create a simple array first and of course we can go with numbers or we can go with string as well whatever we are doing with numbers will also be applicable for string here so let's create a simple array and with this i will have an array of numbers so let's say we have five seven two four and so of course this is an added right and if you can print this i can say log and i can send nums the only thing is let me just run this code so the only thing is you will get this array as it is you will get all the elements in one array but what if i don't want the entire array i want to i want to take one element from this array so uh you'll be saying hey it's simple right you can simply use the index values uh so here let's say if you say index 0 of course you will get 5 right if you say index 1 you will get 7. but then what if i don't want to do this way what i want is i want to take this array uh see there are chances that you might be sending this array to a particular function to perform some operation or maybe you want to send this add on the internet so let's say from the server client will receive this added now of course client can also use index values but what if this array get destructured and assigned to four different variables something like maybe i want to assign 5 to a 7 to b 2 to c and 4 to d now you will say hey it's simple right you can say let a is equal to nums of 0 and then likewise you can say for b c d and e that's one way the other way is the video which is all about is destructuring what we can do is we can put those four elements which whatever you want to assign let's say b c and d and you can assign the array to this so you can say nums or you can say a b c d and we are assigning to nums now what will happen is this adding will get this structure and we will be assigned to these variables these are four different variables here okay let me prove my point by printing them so what i will do is i will say log and let me print d okay let's see what happens if i print d so you can see we got the array as it is which is printed because of line number two here but i'm also printing this four now why this four is because i'm printing d and d has a value which is four quite simple right okay this looks cool what about i want to print c will this work let's try and it's working right so you can see we got two now what if i don't want to save this c i just want a b and d can we do that let's say i want to print d but i don't want to collect c okay so maybe i want to work with a b and d so can we skip c ah the answer is yes you can simply add a extra comma so the moment you give a gap there it will understand hey you want to skip the third value i will do it for you so there's no c variable now it's only three variables a b and d now is it composite to put that comma there yes when you put a comma that means these these are fourth element but if you remove the comma and if you try to run this code uh you can see it still works but the value of d is two where i wanted it to be 4 so that's why you need to put that extra comma which will make sure that you will get 4 as output so this is working what i will do is i will just remove this part for time being so that we can see one single output and you can see we got 4. so even this is working can i create a array of string of course we can so what we'll do is let's do that later as of now i just want to discuss one more thing let's say i have two variables let me just remove this let's say i just have two values here okay so we got five and seven or maybe i will just uh go with two different variables i will say i know this is not a good way of defining variables like a or b but time unit works so let's say we got let a is equal to five and b is equal to six now if you remember whenever you learn a new language or any language for that matter you do this type of examples which is swapping of two numbers how do we swap two numbers of course it deserves a separate video right uh how do we swap two values in javascript a very separate video but can we do that here are there different ways of swapping two variables but here what i will do is i will use added destructuring to swap these two values and it should be in one line so the way you can do that is by using the syntax i can say a comma b so just observe what i'm doing and i can say b comma a and now what is happening here since this is added destructuring so basically we are taking an array here which has two values b and a so value of b is six the value of a is five which is getting assigned two different variables i mean of course the same variables but it doesn't know that right so you can see we've got a a is getting assigned with 6 and b is getting assigned with 5 let's see what happens let me just print the value a comma b and just remove this extra spaces here and let's run and you can see we got six and five that's how you can swap two values not a best way of using this added destructuring just to swap two values but why not when you have a choice as a programmer we always look for cool things to make things easier and what if we can do certain things in one line that will be great easy to manage right okay so we talked about how do we swap two values as well but now i just want to do something else with strings so let me remove this part and this time i just want to create a array of string okay so what i can do is of course i can also do this with the help of split method so what i will do is i will say let and let me create a array of let's say words that's the area i have now this add a has to be created with a string so let's say the string is my name is naveen reddy okay so i have this string and then i have assigned to to a variable here but i want it to be added so example when i print words it should not print in a string format it should be an array format the way you can do that is by using a very special function or a method which is called split and in this split you just have to pass okay so you just have to split this string but based on what uh so i can say split this based on a character which is space and if you underscore you can see we got an array which is separated by a space cool so once we got an array here what i want to do is of course this words is an array now right so i want to take all these elements of course so just to reiterate this words is not a string now it's an array which has five different values my name is naveen ready five values right now i want to assign this to variables of course i can take a b and c as well let's say that as well so let's take that so let's say a b c d and e and i want to assign to work will it work of course it should why it will not work so let me print a and b just two different values and let's see what happens uh just let me put a semicolon not compulsion but let's follow that and you can see we got my name so my stored in a and name is stored in b i can also print my actual name so i can say d and e and you can see it prints naveen ready so that's how basically you can store the values i can actually do one more thing what if i want to store my name which is noven reddy in a separate array so of course it should be a b c let's say i don't do what want to save that is so i can simply skip it right we have seen that and d e should not be two different variables it should be one variable having my name if i simply write d it will not work the reason so let me just try that let's run and you can see we only got naveen what i wanted is navin ready so this d should have davin ready maybe if i have something else here maybe my can we add something here maybe i also want to add talisco uh so now we ready to just go uh that's not my certain don't worry so here how do i get these three values in this one particular variable i want it to be a separate array so what we can do is we can use a special assignment or can use assignments special operator here which is three dots so when you say three dots it simply means rest of the elements so this a has taken my b has taken name the md element which is not mentioned is taking is which we can't use anyway and this thing will use navin ready telescope okay you might be thinking just because i have three words that's why i'm using three dots not exactly it doesn't matter how many words you have here how many elements you have it will be always three dots even if you have ten words it will always be three dots so let's run this code and let's see what happens and it worked can you see that we got naveen reddy the disco uh let me add something else here i will say log because we also have a blog channel so now we have four words right uh will it still work just because of three dots it works doesn't matter how many words you have the dot dot dot means rest of the elements so that's it from this video i hope you understood what is the structuring of an array in the next video we'll try to understand how do we destructure a object in this video we'll talk about array methods we have talked about arrays and then we were able to print all the values of the array with the helper for off loop but then there are some methods which we can use to achieve that okay so we'll talk about some more methods in fact we have talked about splice method uh push pop this time let's explore more specifically this is more for the values when you have huge amount of values and if you want to do some operations on that the famous one is the map reduce or the filter so we'll try those methods here in this example or in this video but initially we'll start with the for each method and let's see what that means see first of all when you talk about the other in fact we don't have an array yet let's create one add it so let's say we have an array of nums of course you can go with any type of array or any name of the variable here but as of now let's go with nums and then i have few values here and let me just go with the complex values let's say 42 51 uh 24 maybe 898. i'm just going with some random numbers 65 and 12. so you can see how many values we have we have six values here right now with this six values i just want to print all the values now yes we can do that by just printing this array as it is you can say log and you can mention nums the moment you save this we can just go back here and say one and you can see we got the array as it is okay but what i want here is i want to print each value individually because if you have that power of printing the value individually you can perform some other options as well maybe you want to multiply the number by sum or the number or you want to perform some other operation important thing is i want to get this value one by one you can do that with the help of for offload which we have seen before as of now let's try to use a method of an array so array which is the added numbers here has a method called for each so now what this for each will do is foliage is a method which will give you one by one value so it will show the value it will give you the next value then you can perform the option on that so basically for each says i will give you one value at a time now depend upon you what you want to do with that okay but then how we are going to do something with that see the thing you can do something in a programming languages with the help of methods or functions right so this for each will take one value and it will pass that value to a function now you have to accept the value in a function and you can perform any operation so basically what we are saying is we need to define a function inside a function okay so this this for each is a function of these nums and in the bracket we have to pass a function now we can go for the anonymous function oh the adder function which we have talked about so we can use that so let's use an arrow function let's give a bracket and let's use an arrow so this function will take a value and it will perform some operation so we have to open the curly brackets for that because this is a function i just hope you remember error functions uh this is another function here so what we are doing is we are trying to create a function inside the function as a parameter here i know that's tricky initially but when you try it by yourself it will make sense okay uh but what value have to accept so this for each will give you one value at a time right so we'll accept that in one particular variable let's say n as of now i know this is not a good way of writing a variable name but that works right so let's pass n here and then this end will be passed in that function now in this function you can perform any operation with this n maybe you can print this value maybe you can multiply this by 2 your choice i just want to print this value here so what i would say is it's a log and let me print the value as it is i'm not doing anything else i'm just taking the value and printing it okay let's see if that works first so before the explanation and it works you can see we got all the values individually printed on the terminal so you got 42 51 24 98 65 and 12 that works okay but then how exactly is working now for the h is the method of an array so it will take one value at a time and that value will be assigned to n now this is a parameter or the argument you're passing now it will go to this function and you can do any operation here as you can see we are just printing it maybe you will multiply maybe you will do something else your choice but here at this point i'm just printing it and that works now you can also multiply this number by two if you want and that should work you can see all the values are multiplied here uh so this is how we can use a for each but again if you remember in either function we have talked about one more thing whenever you have an adder function and if it only accepts one argument you don't need to put a round bracket for that and so you can skip that it will still work okay that's how we can use another function now apart from this or sorry that's how you can use a foliage now foliage can be used for different types of data in fact in the upcoming videos we'll talk about set we'll talk about map and you can use forage there as well okay yeah so that's about in fact you know forage also takes three parameters from not wrong you can pass the value num uh you can pass a value one value at a time you can also pass the index value and you can also pass the array as it is not tried this before let's see if that works so yeah it works so basically in an array you can pass one value which is the value itself or you can pass the value you can pass the index of that value and you can pass the entire array uh how do we know if that works let's print it one by one let's print the number then let's print the index number as well and we'll print the nums the actual array and it should print here it is printing the value which is 42 the first value then just printing this index value and just printing the entire ad as it is same thing goes next value 51 and then the index value and then we got the entire array itself so that's how we can use a for each method that's one of the methods which of the other which we have talked about in the next video we'll talk about the filter map and reduce okay so in the last video we have seen how do we use the method which is for each works for array right and then we mentioned that we have some more methods so let's explore those methods here but before that i just want to do one thing i just want to remove the extra two parameters which we used uh let's keep it simple all right so let's stick to this one so yeah we are passing only one variable and then we are printing it that's it and let's rerun with that particular thing yeah it is working okay so what else we want here see i want to print all the values right that's what we have done and now we have another requirement so the requirement is how do we print even numbers it's very simple actually once you got the value right which is n in this case now this n will be replacing i mean this n will be having the value of 42 51 24 1 by one right so this is a callback function which will be getting called every time you have a new value that makes sense right but then it will be getting called six times for every value it will be getting called and in this particular function we can write the logic and we can use if else here we can say if the value is divided by two if it is divided is zero that means it's a one number we can write that logic inside the method itself or the function itself the problem here is we will be calling this function six times for every value now you might be saying that's okay right we are just printing the value right we are not doing much yes that's true but what if this is a function which is very loaded there are so many statements there are so many variables there are so many operations are going on and this is not just only six values but if you have thousand values so imagine for a thousand times you're calling this function and of course not every called will be useful because we want to just use only for even numbers right and so i don't think this is a good idea right so what we can do is before going for forage before printing the value we should be able to filter these values and that's right we have a method called filter in array how that works so what we'll do is before forage we will first filter the values so we'll say nums dot filter okay now this filter will take one parameter same as for each so it will take one parameter let's say n again of course this two ends are two different variables okay this n is local for the callback function of filter which is your passing here and this is a local variable for the callback function which you are using for for each two different functions so we can use the same variable so let me use the error function here now what we are doing here is we are saying it will pass a value the only thing we have to check is actually if this value uh actually we are we want to return the value which is true or false now since we only have one statement you don't have to maintain return keyword but written is there okay if we are returning true or false filter will allow the value only if it is true so do we check so for this particular n i want to check if it is even or not we have seen that if n mod 2 is equal to equal to 0 triple equal to 0 if this is true that means it will pass true for each value okay initially the value of 42 is it even yes it will return true 51 it will return false 24 it will return true so for every true and false it will send that value ahead so what i'm saying is if you try to print this i'm not sure if you can really print these things let me try i've never tried this before let me comment this section uh you know java trick is actually a very tricky language to work on so let me print and let's see what happens okay so i'm trying to print this on the console and this is what we got okay so you you can see we got all the even numbers so filter basically collects all the values and then it will return that as an array itself so the output of this is an array and we have filtered it now you can perform filter by anything you want maybe a single operation like this maybe a complex operation the only thing is if you have multiple steps you need to open the curly brackets right you have to open the curly brackets here and close it and you can do whatever you want there maybe ten lines of code hundred lines of code thousand lines of code that's your choice it just needs true or false okay now once you've got a filter let me just go back to my earlier code yeah now once you've got a filter on the same values which is here after the filtering you can simply apply the for each and that's right we can do this i know that sometimes it looks weird but it works so basically what we're doing is first you got nums which is an array of six values on that you are applying a filter which will only return four values and then on that four values that you are just printing it and that's it it's so simple right and let's see if that works and yeah it works you can see we got all the values one by one so this is working now what next we can do so the next thing is we have done with the filter now it's time to do something else what we can do is for these filtered values i want to double them so this 42 will be let's say 84 24 will be 48 so i want to double all the values how do we do that uh of course we can just in the for each we can just say multiply by two our job is done right that should be simple and you can see we got all double values but sometime you it can be a complex operation and maybe you don't want to do for each you you just want an array with double the value in that case you can apply another method which is map so map what will do is it will take a value it will change the value that's why it's a map okay so take a value change it something maybe double the value maybe subtract it maybe perform some complex operation maybe save somewhere it doesn't matter we just need to map it so in this map it will take one parameter which is let's say n again again as i mentioned this all these ends are different variables with the same name but it works you can have a different variable or same name variable it doesn't matter now on this map what you want to do is you just want to multiply by two that's how you double it so take a value and double it so filter it will remove it will filter the elements based on the condition we are saying only even numbers it works now on that even numbers the numbers will go ahead and then on that even numbers you are applying a map so you are taking each value and you are doubling it with two so if it is 42 it will be 84. and once you are doubling it you are just printing it i mean though this the same array goes for the for each and you are printing all the values let's try that works and yes it it works you can see that simple right so filter basically takes a parameter or takes the argument it will check for the condition if that is true it will send that value ahead now map will simply take a value and double it and for it will take the value and print it that's what we have written there right cool so these are the three methods in fact we can use one more what if i don't want all the values here what i want is i want the addition of all these values 84 48 196 24 how do we do the addition see after addition you will get one value right so basically what you're doing is you're you're reducing the value and that's where we have this very famous method called reduce so we'll not be using for each because for it will be applicable when you have multiple values reduce will give you only one value so why do we need forage so instead of foliage we'll use a reduce method uh i don't know if you have heard about mapreduce if you don't know about it just google map reviews by google google they have written the theory of it or the paper okay so mapreduce map and reduce first you will map it and then you will reduce it to one single value now what we are trying to achieve here is we want to add all the values how do we do that uh so basically when you want to add something at one point see even if you want to add 42 51 24 you have to take two values at a time right you can't simply add a 42 with which what value you will add it so you will take two values 4251 the addition will be added with 24 and then that edition will be added with 98 or whatever the value is so basically we need two parameters right so we'll take a and b you can see that we are using different variables this time now with this a b you got two values right you want to perform some operation and the operation we want to perform here is a plus b we want to add the value that's it and then whatever the addition is stored here uh you will just save that somewhere that's a result so because this videos will return one value right and you're saving that result and here i'll just try to print result i'm not sure if this works let's try oh it works if we we're not even sure if that's right answer maybe you can use a calculator to check that this is uh 1 32 and it looks the right answer right again you can check that out and let me know in the comment section if this is wrong okay or maybe we can just use a simple value just to calculate it just faster so we got how many even numbers let's take three even numbers so 2 4 and 6 2 4 and 6 is 12 24 the answer should be 25 this one this code and that works so this is how you reduce it uh okay so this is how it works so we got filter map reduce and we also seen for each in the earlier video we have talked about arrays then we have talked about some methods of addis right we have seen filter map reduce that works well now when it comes to arrays you can have a collection of values and every value will have an index number right in this case be this one will have index number zero two will have index number one and this goes on right and then arrow also supports duplicate values maybe if you have let's say instead of five let's say we have three so array does support duplicate values now one of the data structure we have is called a set now set is very famous in other languages in javascript it was introduced as a class in es6 so now we can use set which will have only unique values so if you want to differentiate between set and add a set will have set is a collection but then all the values will be unique next set normally is a unordered list which means if you so it will not maintain the sequence in which you have added the elements but in javascript it maintains the order but again it is unique and it will not have an index value so the way you can use index five to fetch the value from the array you can't do that with set so let's see what exactly set looks like so basically set is a class right so we have to use the constructor to create the object so will you say maybe we can have a set of values again with some nums but this num this time is not a normal array it's a set how do we create a set now since the class we can say new set okay that's how we create an object of a class new and then the class name and the curly brackets this is basically called a constructor but we'll discuss that later okay now once you've got a set with you you can assign values here in fact you can pass the value in the bracket itself what i will do is i will take a string okay so maybe a string like bookkeeper so you can see we got a set and then we are having this array basically this is a string and internally this string will convert it into our individual values so because basically we got a set of characters and now let's print this characters and let's see what happens uh so in the comments section write the answer what what is your guess if you try to print nums i don't know it's not numb anymore it is a string but anyway if you try to print these nums what will be the value so i hope you answered and let's see the answer and you can see we got a set of six values oh y6 it's one two three four four eight ten so total ten characters are there in this string which we mentioned bookkeeper but it is saving only six which is b okay e p r that looks good the only thing is you can see as i mentioned before in set you will not have repeated values that is what is happening here we don't have repeated values we have values only once that's outside works okay now if you don't want to assign the values here we can also assign values later maybe i want to assign some values we'll say nums dot how do we add a value with the help of add method we'll say add and then we can pass a particular value which you want to save maybe 2 3 [Music] three let's say three and i can just copy paste this multiple times let's have different values i will say three four okay let me repeat that three and let's see what happens and here let me insert my name just to check if you can have different type of data i will say naveen kiran and chillock so we got these values and let's see if that works we can just run this okay when i print this num so you can say just printing 3 4 that's right it is not printing three again because it is repeated value then we got naveen kiran and chirag that works so basically this statement here has no effect on the set okay nothing is happening here but the question is how will you print these values how will you print all these values here uh okay now for that what we need to do is we need to use a loop we can use of off loop for off loop in fact you try that so in the comment section write the answer how do we iterate in this set with the elbow for off it will be helpful for those who don't know how to do that right so we'll use a for each loop here so we said num start for each the same thing we have done for the arrays now you tell me what i should mention here uh should i mention only one value let's try that with only one value and let's see if that works so basically in this it will accept a value maybe i can say let's say value and this value will be printed right so let me open the curly brackets close and we'll say log let me print the value and let's see if that works let me just clear the screen and yeah you can see that it is working this is how you can iterate on a set so this is one way uh the another way of doing this what if you want to pass three values the way we have done in arrays uh we have to pass a value we have to pass a index value and then we have to pass the array itself now here the problem is we don't have index values right example if i try to print value and i have to pass the index value for that what we can do is we can we don't have index value so we can say value one again we'll discuss what this value one is and let me pass the num as it is and you can print value one you can print num so you can try that but the question is what is this value one here we don't have index values in set right uh so the answer is when you say value one here it is a repeated value this value these values they are two these are the same values since we don't have index values this is your repeated values just to maintain that uniformity between arrays and set but logically only one value works so we'll go with that okay in fact in set we can do one more thing instead of printing all the values let me print one value and i just want to check if that particular values is there in this set let's say i want to search for two do we have two in this set so what we can do is we can say nums dot has there's a method called has in which you have to pass the value so i will first check for 3 because 3 is there it should print true and that's why it is printing true but what if i print or pass 2. in this case you can say it says false but if i pass a name like naveen it should be there which is that if i change with n small n now this is a case institute as a string because string is case scenario uh and you can see we got false so yeah that's how you work with set now in the next video we'll talk about map so i hope you got some idea about set where set is a collection of values but the only thing is it will have unique values it is ordered in javascript not in other languages and it doesn't have the index values in the video we have talked about set right so this is a data structure which was introduced in as6 and with that we got one more which is map now map looks similar to object but we'll discuss the difference between map and object later as of now let's see what map is see when it comes to arrays we can have a collection of values right and every value will have an index which is a number then we have a set with which we don't have the index values but it can have our unique values now map says you can have values but every value need to have a key which will not be a number which can be a number but we don't need to have a number you can have a string you can have an object you can have anything else which you want so it's basically a key and a value pair that's how generally you store data in database that's how you store data in any format nowadays it's a key and a value pair so map becomes a very powerful tool of storing data and to work with data basically so how do we create a map now first of all map is a class so we have to use so i have to say let's say map itself we can also say phone book but let's say map select map is equal to new map that's how you create a map here now how do we add a value in a map so basically as i mentioned map will have a key and a value so we can say map dot how do we set values or add values with the help of set method so we can say set and we'll discuss why you set why not add okay we'll discuss that so we got map dot set and then we can have a key and a value now this key value can be anything the key can be a number and value can be a string or key can be a string value can be a number and vice versa and then you know both can have both can be numbers good both can be string both can be objects it doesn't matter uh we can have a set list and this time let's go with names so i will say this is the set of people with their favorite technology or the skill set they they have now uh so we'll start with naveen so this is the first person and the skill set is is let's say java and then we got one more which is uh set i will say kiran and again inferior technology is let's say android and map dot set the next name is let's say chirag and the third technology for chirag is machine learning and as you can see we got three technologies here right and then this is your map where you have a key and a value pair now what you will do with this key in a value pair we can do multiple things first i'll let me get all the keys here so how do we get all the keys let me just let me just print all the keys so i can say hey map using this method of keys i can fetch all the keys now this key here davin kill and chirag are the keys and you got it can you see that we got uh naveen they are the keys here right okay this is working okay uh so we can get the keys what else we can get here we can also get uh we can also check if a particular key is available in this map and for that we can use a map dot any guess what the method name is that's why it has we have seen that before right so we can say has we can check do we have chirag here let's try and you can see it says true that means trilog is there so we can use has we can get everything in fact what if you want to get the value so in that case you can say get so you can say map dot getting in pass direct which is a key it will give you a particular element you can see we got machine learning ml okay that works what else we can do here what if i want to print all the values okay not just a particular key and a particular value all the values in this case we can use a loop and then we know we can use uh for off loop or we can use for each we have discussed that before so here let me just use a for off loop so i will say for let now the problem is we don't have one value right we have two things we have a key and a value so i want to get key and value both so for that i can say k comma v of course you can go with any variable names you can say key value itself or you can say kv that works so we can say kv of map and now let me just print the values how do we do that it's very simple you can say log and here let me print the value of k first and then let me print the value off and then we can give a space in between or maybe a colon and i can say v that's it we are just printing the map and you can see we got all the values we got naveen java kiran android girard ml so that's how you can assign the values that's how you can print the values now my question is what will happen if you try to do one more set here and uh let's have let me just repeat kiran there and let's say the current technology or maybe let me just repeat navigate because following technologies changed from java to blockchain okay so here we got map.set naveen blockchain here now you tell me what will happen if i run this code will i get four values or will i get three values if you get three values then navin will have what java blockchain so let me know your answers in the comment section and let's try it out and if you can observe the new value for naveen is blockchain java has been replaced and that's why we are using set here not add we are not adding a new value we are updating the value if it exists and that's why it says set if you are coming from java background we use put we don't say add but in javascript this is set so put set almost same we are not using add method okay uh this works can i use for each loop let's try so in for each if i say map dot for each uh what do you think how many parameters you have to pass so basically you have to pass two values a key and a value right so we'll say two values will say key and a value again the same thing and we can give a bracket open and here let me print the value let me print the value key first with the same syntax okay uh now will this work see logically it should but the problem is if you remember the syntax when we have written array for each array when we are passing three values remember the sequence the sequence was value then index number in the same way here it should be value first and then the index value this is the only tricky point because in forage you pass the value first then the index value and then the set if you want to pass here we are just passing the k and a value so first you have to pass the value then the key and i hope this will work and that worked can you see that so that's how we can work with map so map is basically a collection of key and values key value pair okay now the similar thing is available in object as well right if you remember object properties they are key in a value pair the difference is when you work with normal entities if you if i want to represent a particular object one object example let's say this remote so this remote is one object right so these properties will be the color the length the brand the controls so brand lg color white so we can specify all these properties but whenever you have a collection of values let's say names and technologies or maybe a brand and versions so in that case we can use a map to maintain the map so again this depends upon the requirements which you want to use okay both provides key value but if you have a collection map makes much more sense till this point we have talked about functions right and now we know we can define a function we can write some statements and we have to call a function in this video we'll talk about what is a recursion now if you have learnt recursion in some other programming languages that's completely fine you can skip this video and you can head to the next video but if you don't know the question if you want to revise your concept this is the video okay so basically what is recursion is so let's go back to the function so let's say we have a function here a very simple function so i will say function show and now this function show will have some statements of course and maybe i would just want to go with one statement which is i want to print something on a console and what i want to print i want to print hi that's it just to keep it simple and then of course if you have a function you have to call a function because if you don't call a function it will not get executed right so you can see i'm running the file there is no output because it's not getting called so the important thing is you have to always call a function and let's do that now and if you underscore you can see we got high so at least now we are calling a function which is which is printing high now basically if you look at this function show so of course we are calling the function show here and then from this show function we are calling another function called log so what is happening behind the scene so you can imagine there's a box okay so this box will have all the function calls example when you call this function which is show so currently the show function is getting executed right now this will be in a box this show function is calling a log function so of course this function may be having multiple statements but then when it is calling a log function the current function which is getting executed is log maybe log will take 10 seconds 20 seconds to execute now till that point when log is executing the show function is still there in the box but then show says okay you first complete your work then i will get called right then i will get ex i will complete my task so this is basically called stack right so when you call show so show is on the top now and then show is calling log so log will be on the top so which function will get completed first is depend upon the stack right so last in first out so log was called last so it will execute first and then it will go out of the stack and then it will continue with this show a function right example let's say if you call this function show from some other function let's say i have a function here which is function abc i know that's a weird name for a function but let's go with that for this particular example and then from this abc you are calling a show so go with the flow we have abc function which is calling show and then of course i can call abc function so what we are doing now is we are calling abc functions in the box we got abc then abc is calling show so abc will still be there because it will first complete show because that's that is last in first out right but show says okay first i have to complete log so log will complete first then show then abcd stack and we can call this a call stack cool right let me remove this code now and let's go back to our show function okay so basically we are doing this but my question is from a function we can call another function that makes sense can we call a function from itself example if i say show can we do this theoretically if you think about it of course it's a function right you can call any function you can call itself right now what will happen in this stack is when you call this show this is in the stack then this show is calling the show itself so now we'll be having a new entry in a stack then this show will be executed again it will also a new entry a new entry so the call stack will have all the show functions right and let's see what happens if you run this code you can see we got all the highs because it is calling itself you know show is calling itself you know just to have some fun i will link a video of python on the top of the video so i made a video on recursion python as well which is quite fun i enjoyed making that video uh you can watch it as well okay so you can see we are printing the same thing high and high multiple times but at the end you got a error the other says maximum call stack size exceeded of course right you are calling itself so the below function you know all the show functions which are down they are still waiting for the above function to get completed and that's the problem starts because till what point you are reaching because there is no stopping here maybe the call stack is thousand two thousand one i mean there's no limit right so of course box will have a limit memory size uh so that's the error here but we want to know at what point it is breaking so what we can do is we can pass an initial value uh maybe i can just make a count right every time i gets called not sure will this work let's try so i will say let num is equal to 1 initially it will have it 1 and then with hi every time i will just print that num right just to print the value as well and then after printing it i will say num plus plus and then i will call show this makes sense let's clear it and let's see at what point it breaks so from this code you can see it is calling multiple times and this is a breaking point it's 11 299 so this is the maximum size you can go for okay let me know at what point in your system it is stopping i'm not sure if it is dependent on on some os or engine so if you are doing this on a browser let me know at what point is getting stopped so let's try this once again and just to check if it is breaking the same time yeah so on this machine i'm not sure about this is that depend upon the engine so you can check that out okay but you can see it is calling itself till this point so the call stack is getting exceeded that's the other which we got now this concept is called recursion because a function is calling itself but where do we use this just for fun uh not exactly see there are some scenarios where you have repeated tasks right so when you have something repetitive by calling itself uh maybe you have a json data a lot of you know we have object inside an object you have an object inside an object we can use the question there uh we can also use recursion one for one of the very famous example which is uh the factorial so next video we'll talk about how do you do factorial with a recursion concept right uh so basically this is the question function calling itself there should also be a breaking point maybe one of the breaking point we can do is we can check if the num value is less than or equal to one one nine nine nine if that's the case call show otherwise stop calling show then of course will not be getting the error or not 999 just 299 okay so now if you run this code uh okay let's see yeah so you can see it is getting stopped no error because we are stopping at the right time so we can actually control till what point you want to call this this will be useful when we start when you go for the next video in this video we'll talk about recursion or we have talked about that in the previous video right okay so we'll try to solve a problem with the help of recursion and for that i will use a very simple example which is factorial so first understand what is factorial here so let's go back to the slate and let's try to understand what is factorial so first of all how do you find a factorial so what is factorial so let's say i want to find the factorial of 5 now how it works is this is the symbol we use right we use exclamation there okay so how do we do that so we if you want to find a factorial of 5 it's very simple you do that by saying 5 into 4 into 3 into 2 into 1 right that's the formula we use and once you solve this of course you will get the answer now if you want first of all the answer is 120 you can just write out i guess it's 120. okay so that's the answer right so once you want to find the factorial of 5 this is what you do you try to get the answer okay but if you want to represent that in a numerical format or the mathematical formula so let's say i want to find the factorial of n right so i just want to generalize it so what could be a formula for that so that is n into so if you look at four that is n minus one right and then you multiply this by n minus 2 you multiply this by n minus 3 and then list goes on right you do that till you have n minus n factorial now what about n minus n n minus n is zero right so the idea is the value of a zero factorial is one okay that's for sure you can just take that as as it is so value for zero factorial is one and now uh if you try to understand this into one more parameter is if you look at this thing you know when you say 5 into 4 into 22 into 1 if you just observe this pattern here 4 into 3 into 2 into 1 can you say this is 4 factorial looks like right because when you say 4 factorial it will be 4 into 3 into 2 into 1 how about this part the part from 3 to 1 it is 3 factorial so can i actually say is 5 factorial is equal to 5 into 4 factorial we can right in the same way if you want to represent the mathematical formula you can say n into n minus 1 factorial so basically what we are doing is we are trying to find a factorial with a number and then the formula is that number with the number minus 1 factorial it's that simple okay uh how do we use recursion here if you think about this this is actually the question right you're finding n factorial for that you have to find n minus 1 factorial for n minus 1 you can say okay it's n minus 1 so if you look at this one this is actually n minus 1 into n minus 2 factorial so basically if you define this thing as a function of n we just have to call the same function again and again simple right the same thing in the code now how will you do it it's very simple uh okay how do we do that i'm not sure uh okay so if you want to do that which we have written in the slate into this code first of all we have to get a function right using you can find a factorial of a number how do we do that it's very simple you say function and you define a function name as fact and you can pass a value let's say let's go with n here and then in this function i want to perform the operation basically i have to find the factorial itself so basically i want to return n into n minus 1 factorial that's it this is what we have done in the on the slate right so n factorial is n into n minus 1 factorial if i can get that i got my answer okay let's try uh so basically here first of all we need a number so let's say num is equal to which factor i want to find let's see let's go with five just to keep it simple and i want to save that result in result itself and which function i was going to call i will going to call fact by passing num simple stuff right and at the end i want to print the result and that's it that should be the factorial value now the question is will this code work let's try let's try and see what it says if you run this code you got an error it says unexpected token the exclamation mark see that's the thing right we have to define this thing we are saying n minus 1 factorial don't you think we are doing that with the elbow function so instead of doing that we can simply say fact that's the factorial of n minus 1 the same thing right n minus 1 factorial is same as fact n minus 1 because that is what we are defining here okay and this should work now i feel this should work okay let's see what happens if i do that if i do that oh we got an error it says maximum call stack size exceeded can you remember something in the last we have talked about right right basically what we are doing is we are calling a function within a function this is the question and of course we do this continuously all the calls will be there in the stack and there will be one point where the stack will be full that is what is happening here okay so we have to stop it somewhere but at what point you're going to stop so if you look at the slate we are supposed to stop at zero because zero factorial is one okay so we have to stop it somewhere how about this we will do this we'll return in the else part and in the if part will check if the value of n is it should be if it is equal to zero because at one point when you say minus minus minus at one point the value of n will be zero so at this point just return 1 that's it and then else part so what will happen now is for the factorial of 5 we are saying because 5 is greater than 0 so it's 5 into 4 factorial right so if we are finding the factor of four okay so how will it look like now is when we are calling a factorial for the first time so it will say fact of five so basically it will go into a stack so imagine this is the stack so this will go into stack and then when we say factorial of four it will create one more entry in a fact we'll say fact of four now this function is still into running phase okay this is still running and then i mean running with it is actually waiting for a factor of four to be end now for factorial four this the same thing will happen right so when you say factorial of four it will go inside and it will check uh is it zero no so it will say okay four into factorial of three so there's a new call of factorial of three and then there will be a new call of factorial of two the new call of factorial of one and then there will be a new call of factorial of zero that's the call stack you have right now this will return one so this goes here this will return one this goes here this will return two because two into one is two and then this goes here this will turn six this goes here so now we got six into four which goes here which is five into what is six into four it's 24 and then the answer ultimately you will get is 120. that's how you end the factorial thing that's the question function calling itself and now let's start on this code and let's see what happens so if i say enter and you can see we got the answer so that's simple okay so basically that's how you got the answer right now there are two versions of recursion factorial recursion in this playlist you can also in this channel actually i have made a recursion video for python as well you can check that out with a different style i tried something else this time and whatever you prefer that's your choice okay but yeah we got the answer so every time you want to find the factorial the beauty is you can change this value to anything maybe four and you can find a factorial you will get 24. the only problem is you can't just go with a very big number so i tried with 179 i think it works with that number oh even 179 doesn't work there's a last number which you can try i forgot which number it was it's somewhere 70 maybe 170 is that number oh that's right so it is 170. so even if you drive a 171 uh the range exceeds and you can see you've got infinity but yeah so you can this is the big number okay very big number it's epsilon of 306. 10 into 306 that's a very big number so yeah that's it from this video i hope you enjoyed it in the comment section and do subscribe for the videos bye [Music] you