Transcript for:
Understanding JavaScript Variables and Types

Variables are like containers, the containers where we keep household items. Flour, pulses and rice are kept in containers at your home You would know what I'm talking about. But how is all this related to JavaScript? JavaScript also has containers called variables. Different data types can be stored in variables such as string, number, array that we'll learn about soon. We'll see what exactly a variable in JavaScript is, how to make it and we'll also do some sample programs. In JavaScript you can change a variable's type in runtime. That means if you make a string in JavaScript, later you can store numbers in the same variable as well as arrays and objects. These are known as dynamically typed languages. Dynamic typing means you can change through run time the type of any variable. But static typing means that you'll have to declare the variable type if it's an integer, floating point number, string or character and only then can you perform operations on it. An example of a statically typed language is C language but JavaScript is dynamically typed because allows changes in types in run time. We'll now go to the computer screen and see how all these things work. We'll see action too, we'll make a repl and in that repl, step by step we'll see how to do all these things. Let's get started. [MUSIC] Many of you were very happy with the last video and that made me feel really good. We'll make a new repl now. By the way, you can also follow people on repl.it. Some of you have followed me and if you haven't, then follow me on repl.it. It makes me very happy when you guys follow me. It makes me feel very sad brother. So, you can follow me on repl.it. I'll click create and create a new repl for this video. I'll simply write node.js here. I'll select node.js and name it 02_Variables. And data types, I'll let it be variables for now. I'll click on create repl. I'll add this to the ultimate JavaScript folder so that things are extremely organized. But before that I'll write and show you some code and before that, I'll open our pdf which I have provided you. I'm explaining the basics now and I'm starting with them because I want that even a beginner should understand. Like a box, this is my box. It has my earphones. It's a type of container and it has my earphones. I put in my earphones and closed the box. The way earphones can be kept in this box, Can I put this fan in the box? I can't, it's a small box. Will this AC remote fit? No, it won't. This can't and many other things also won't fit. As these things won't fit in the box, JavaScript also has some similar boxes in which you can put in a particular type of data. These containers are called variables. Here, you can put numbers or string, like a name or a collection of numbers, of numbers and strings. So, a variable is simply a container in JavaScript. Now, what is this container? There's some space in memory. For example, I have this room. I can cook here, I can clear out some space. You can take this room as memory or RAM. So, what are the spaces you cleared out for things? Those are your variables. I've said the same thing here, "Just like we follow some rules while speaking English, (the grammar) we have some rules to follow while writing a JavaScript program. The set of these rules is called syntax in JavaScript." So, we have to follow some rules known as syntax. For example, I had told you in the last video if I write console.log This repl contains code for the Ultimate JavaScript course. Video number 2. If I write this and run it, it'll get printed. If I write console.tullu instead of console.log, will it run? No, it won't run. Many people would be wondering of there's a function called console.tullu. No, I was just showing that a wrong syntax won't run. Suppose I write any nonsense here. Hi how are you. Suppose I add some nonsense here. The first line is fine but if I add unnecessary things. If I write something in between console. Then this won't work at all, see the error. It'll say what are you doing, are you drunk. So, it won't run like this. I'll correct this code, the correct code works. In this way, we have to follow some rules in JavaScript if we want to execute anything. What's a variable? It's a container, as I said. Our memory, which I'll show you. If I go to performance, you can see the 64GB RAM. Many people will have 8 GB too, in mobiles it's sometimes 512 as well. No, 512 is for laptops, not mobiles. Many people use them, I have one too. So, it depends whether you a 512MB laptop or 64GB one. So, your memory will vary. Variables are made in memory when program is executed. We use repl.it which runs on cloud. So, this doesn't apply to repl.it. Repl.it allocates the memory available on cloud. I'm using it so that people on phones or slow laptops can also follow along as this works on browser and runs on cloud. Don't worry about how this works, we'll learn JavaScript. I've written, "This is very similar to the containers used to store rice, water, and oats." Treat it as an analogy, many people take it seriously. Many take it to heart, kidney and I don't know where else. "The value of a JavaScript variable can be changed during the execution of a program." If I want to make a variable and write var a = 67. A warning for you, I've written var but we won't use it. Just know that var is used to declare a variable. It'd be better to use let. If you've seen any old JavaScript code, you'll see var being used. We'll use let to make a variable in JavaScript, forget var. a contains, sorry, a contains 67. I'll do console.log a and I get to see 67. I'm explaining very basic concepts here and this is for those who don't know JavaScript at all. So, 67 got printed here. But if I write a = harry, the type is now string. As its type is string, it holds harry. If I run console.log a, will 67, harry get printed? Absolutely yes, that will be printed. Some languages allow this, and some don't. We can clearly see that JavaScript allows this. It allows us to change a variable type in run time. That means I put 67 in a and then I wrote harry in it. If I put some other type in a, it'll still work. So, it's not that if you make a number containing variable in JavaScript, then you can only put numbers and not strings, you can. But I'll give you an example, if you try it in C language, then it'll show you the door. It'll tell you to get out, it'll show you an error. So, you can do such things in JavaScript. So, I'll tell you how this works. When we write var a = 7 or let a = 7, I'll explain var, let, const soon. Don't get confused, you can use var too. Even if I use var, nothing will change. But we'll discuss the difference in var, let, and const. I want to build a strong foundation and continue because those who know nothing will not understand. I want everyone to be able to understand this. When I write var a = 7, the 7 is literal. I'll zoom in a little. This is the process of declaring variables. a is called the identifier, you can give it any name. The = is an assignment operator which stores the 7 inside a. I could've stored a @ or a string literal in double-quotes. What does literal mean? A constant value, like 7, or "Harry" is a literal, it's a string literal. There are some rules to name a variable. I'll tell you about those rules. Letters, digits, underscores and $ sign are allowed. You can start with a $. Or you can start with an underscore or a letter. You can't start with a number. For example, if I write let 8harry = 7, it'll say no. Not allowed this will throw an error. I hope you can see my repl.it screen. I'll zoom in, in case someone can't see it. I don't want anyone to complain later. I'll comment it out. I'll run and show you, you can see the error. It says there's a syntax error. I have told you what a syntax error is. I'll comment it out. I'll run the program, I want to show you that it works. It's running, 67 harry. What's the third rule? JavaScript reserved words cannot be used as a variable name. If you try to be smart and name it as var, if I write let var = 7, no, var is reserved in JavaScript. Some names and keywords are reserved for JavaScript. You cannot use those as variable names. Here, I'll comment it out in this way so that my program works. I don't want you to have an error when you open repl. Now see, JavaScript is case sensitive. If I make Harry and then haRRy, they will be treated as 2 different variables. JavaScript is case sensitive, always remember that. So I can make Harry and then HarrY and that would be a different variable and many such. But don't do this in your practical programs. I want to suggest you that because doing this reduces the readability. As you are human, you'll read it as harry regardless of case. Never reduce the program's readability. These are the different variables, I hope you understood. In the next video, we'll learn the difference between var, let, const. It's confusing so I'll take my time explaining it. If you haven't accessed the JavaScript playlist yet, then access the ultimate JavaScript playlist. You can find the link in the description and even if you can't find it there, go to the channel and open the playlist and click view full playlist, it's simple. And then click to bookmark it. Then click here and save it. If you want to follow me on repl.it, you'll get the repl link. Open it and do follow me on repl.it. 300, I have 305 followers. People are following me on repl.it very quickly. So, I feel very good, genuinely I feel great. So do follow me, if you want to. I'll make a lot of things here. It's a good community, you can follow me here. I'll use repl.it in future videos and courses too. Repl.it has a very smooth experience, it's great. So, I hope you understood. You'll get the link to the notes in the description and I'll explain let, var and const in the next video. I hope you understood the concept of variables. Before going, I'd like to add that many people will think that they know all this. I just want to build a very strong foundation. Even if you think you know this, believe me, if you'll always watch my videos, you'll definitely get to learn new things. In the future, I'm going to increase the level a lot. At one point, I'd be explaining such advanced JavaScript concepts that you'll admit that you had fun. So, stay tuned for that. That's all for this video guys. Thank you so much guys for watching this video and I will see you next time. [MUSIC]