Variables, Constants and Data Types | Bondar Academy
Course: JavaScript for Testers
Module: JavaScript Fundamentals
Instructor: Artem Bondar
Lesson Summary
In this lesson, we cover variables , constants , and data types in JavaScript, which are fundamental building blocks of programming. Variables Variables are used to hold information during code execution. They can be created using the keywords var or let . For example: var firstName = "John"; let lastName = "Smith"; Variables can hold single or multiple values and can be reassigned. For instance: let age = 5; age = 6; // age is now 6 Think of a variable as a shopping cart that holds items during your shopping trip. Constants Constants are defined using the keyword const and cannot be reassigned once set. For example: const occupation = "Engineer"; If you attempt to change a constant's value, an error occurs: occupation = "Driver"; // Error: Assignment to constant variable Data Types JavaScript has five primitive data types: String : Text enclosed in quotes, e.g., "Hello" Number : Numeric values without quotes, e.g., 25 Boolean : Represents true or false Null : Represents no value Undefined : Indicates a variable that has not been assigned a value In summary, variables can hold and change values, while constants maintain a fixed value. JavaScript supports various data types for different kinds of information.
Video Transcript
In this lesson, we will talk about variables, constants, and data types. So variables and constants are the essential building blocks of any programming language. And I will show you how to create variables, how to assign values to the variables, and what's the difference between variables and constants. And also what are the data types available in JavaScript. So let's get into it. So let me create two variables first, and then I explain those. So the first variable will be firstName equals to John. And the second variable will be lastName. And lastName will be Smith. Okay, so we created two variables. What are the rules of variables creation? As you can see, I use two keywords, var or let. So in JavaScript, this is how you define the variable. There is slight definition between the functional differences between var or let, but for our scope, it doesn't matter. So you can use either var or let to create the variables. Then you provide the space, and you give the name of the variable. In our example, we just gave the name firstName and the name lastName, and we assigned the value to this variable. So now in general, what are variables created for in any programming language? So variable is a mechanism to hold certain information for you during the code execution. The saved value inside of the variable will be there only during the run time. And once the program is executed, this value is just gone. So the variable is just a mechanism to carry the certain information during the execution process. Think about the variable as a shopping bag or a shopping cart when you go to the store. So when you come to the store, you put the certain fruits or vegetables in your shopping cart, carry this cart into checkout registry, and then pulling out those vegetables from the cart, and then paying for them, and then going home, right? And returning the cart back to another customer who can use this cart and use for any other products in the store. So that's the same thing happens with variables in the programming languages. You can keep either a single value in the variable, or you can keep multiple values in the single variable. It will be called as object, and we will talk about objects later, and so on. So now let's just print this variable value to the console, and type console.log, the known command that we learned from the previous lesson, and we type firstName. And now we execute our lesson. Here we go, and we can see that John is printed. And if I will put the last name over here and execute the same command, now the Smith is printed. So we were able to successfully print the value stored inside of the variable to the console. So what else you can do with the variables in JavaScript? You can declare the variables first, and then use them later. For example, we can create variable age, then separate by comma, let's say, date of birth, and let's say, sex, right? And then we can assign just values. Let's say age will be five, then sex will be male. And let's print the value of the age into the console. So we put the console.log, and we want to print age, right? And we have Smith, and we have age five is printed to the console. And what else we can do with the variables? Let's say we use this variable to store the value five, but now we want to override this value with a new variable. So we just go age, let's say, equals to six. And we can reuse the same variable that's created before to store a new value for us. And I can print console.log and print the same variable age that was before. And look what we have right now. So first we had a five, then we have a six. So we're using the same variable name that we used before, but our variable is holding a new value after we used it once. With value five in the beginning, we override it with a new value and can reuse it. That's why it's called variable, because the value inside of this entity can be different and can be reused with different information. Now let's talk about constants. So constants are defined in JavaScript with keyword const. Then we provide the name, for example, occupation. And it's equal to, let's say, engineer. Okay, and we print console.log occupation. So if I run this code, so everything seems the same, right? So engineer, we see it's printed. So behavior looks like exactly the same as with a variable. But what's the difference? The difference is that if I try to use the same constant that I created and try to assign a different value, for example, driver, and we'll try to run the code, look what happened. We have the error assignment to constant variable that it is not possible. That's the main difference. That the constant, if the value is assigned to constant, it cannot be changed during the runtime. Only this value can be used till the end of the execution of the program. Also, you cannot do things like we did at the very beginning when we created a list of variables that we want to use and then assigning the value. We see the red squigglies, which says const declarations must be initialized. It means that constant cannot be created without the value assigned. If the constant is created, the value must be assigned. Otherwise, you will have an errors and it will not work. So just to quickly summarize the difference between variables and constants. Variables are the entities that can hold any information for you during the runtime and the values for the variables can be reassigned. So you can use the same variable name for the different value. Constants, when created and value is saved inside of the constant, cannot be changed later on. So this constant always will keep the same value until the end of the execution. And let's talk about the data types. So in JavaScript, we have five primitive data types. So let me create a few more variables and I will show you all of those. So here are the five different variables that I created and each variable has a different value of different data type. The very first one type called string. String because it is in the double quotes. So every value that is in double quotes or in JavaScript, you can put it also in the single quotes. It will work the same as a string, meaning it is just a string. So let's take a look at how it works. In the single quotes, it will work the same as a string, meaning it is just a text. So computer will understand the value saved in this variable as a text. The second data type called number. And this number is just a number, right? So the visual difference is that debit in our example in the quotes, but 25 is just a 25 without any quotes or something. If I will put 25 into the quotes, computer will think that this is not a number, that this is a string. It means that computer will not be able to perform arithmetical operations with this text because for computer it will be just a text, not the digital representation of value. The third data type is the Boolean data type. Boolean data type can have just two values, true or false. That's it. So in our example, we created variable, is he married? And which is false. It means for us, he is not married. If this would be a true, it means that this person in our example is who is, is John Smith, right? That we created with the middle name is David. His age is 25. And we wonder, is he married or not? And we say true. Yes, he is married. Or we say false. No, he is not married. So the Boolean data types is used in the logical operations, in the conditions especially. And we will talk about the logical operations and conditions later on. For data type called null, it's just a null. So what null is, is like no value. That's it. For example, if our person is not married, so logically that he may not have any years of marriage to be in our record. He cannot have this value in theory. So instead of not assigning anything, it means that we are missing something. Null will represent our expectation that our person is not married. It means that we are missing something. Null will represent our expectation that we should not have any value for this particular variable. So null is expected result of not having value for the variable, if I would rephrase like that. Just one more time. If our John Smith is not married, he cannot have years in marriage, just logically, right? And the last fifth data type, it's called like that, undefined. Undefined usually you will see in a kind of an error states when we try to refer to certain variable in our code, but it is undefined. So we cannot use something that does not exist. That's it. So if the variable is undefined, it means that we can't technically use. That's pretty much it. So let's quickly summarize what we did in this lesson. So we learned what's the difference between variables and constants. One more time, variables are the entities that can save different types of values for our code execution. And the values to variables can be reassigned. A var and let are the keywords that used in JavaScript to create a variable. And a constant is another type of the entity that can save the information for you. Similarly like the variable, but the value can be assigned to the constant just once. And JavaScript has five different data types, which is string, number, Boolean, null and undefined. All right. Thank you guys and see you in the next lesson.