Lesson 04
JavaScript Variable Definition with const
Summary
# Summary of the Video on Variables
## Introduction
- The video discusses the concept of **variables** in programming, specifically in JavaScript.
- It serves to explain how to store information when writing programs.
## Key Concepts
1. **What is a Variable?**
- A variable is a container that holds data values.
- Data can be personal information, like your name or age, which needs to be stored while programming (e.g., in an application like Facebook).
2. **Defining Variables**
- In ES6 (ECMAScript 2015), we define variables using the keyword `const`.
- Example of defining a variable:
```javascript
const name = "My Name"; // where "My Name" is a string
```
- Once defined, a variable can store a value that can be accessed later.
3. **Variable Naming**
- It's encouraged to name variables descriptively to enhance code readability and maintainability.
- Poor naming practices (like using single letters) are discouraged.
4. **Types of Values**
- Variables can hold different types of data, such as:
- **Strings:** Enclosed in quotes.
- **Numbers:** Not enclosed in quotes.
5. **Using Variables**
- To retrieve and display the value stored in a variable, use `console.log()`.
- Example:
```javascript
console.log(name); // Outputs: My Name
```
6. **Assignment Operator**
- The `=` signifies assignment, which means you assign a value to a variable.
## Challenge
- Viewers are challenged to define a variable called `age`:
```javascript
const age = 35; // Example value, must be a number
- The
age
variable should be a number (not a string) and can be logged to the console.
Conclusion
- The video ends with a recap of what was learned about variables, different value types (string and number), and encourages viewers to practice by creating variables.
Call to Action
- Viewers are encouraged to like the video and subscribe for more content.
Video Transcript
Alright guys, welcome back. Welcome back to MBK world. In this video we're going to be
talking about a concept called variables. Basically we need to learn how to store information
when we're doing our programs. So how does it work? Okay, so before I go on let me clear
my console. There's this nice icon here of the trash can. It can clear my output. And
we're going to start over. We're going to talk about variables. So last time we talked about
console log, how to print a message to the console. We learned about the concept of a
function that takes an argument and as an input and then process does some processing and then
output something. And now we need to learn how to store information. Because we I don't know,
say you have, for example, you're using, I don't know, you're using Facebook, for example, and
they taking many kinds of information, right? They need your name, your birthday,
or interest, like many, they want your descriptions, that kind of stuff. So where does all those
information go when you see that page? So all those guys are inside a variable, they're stored in a
variable. So variable is just some kind of box where you put some stuff and you can retrieve it.
You can access it later. Okay, so the way you define a variable in JavaScript.
Okay, is the following note I'm using what's called a yes six and beyond JavaScript is an
implementation of a language called academic script, which is the standard for the JavaScript
language. And I'll be using the ES six, so called academic script 2015. And before that, the language
had some constructs. And now they added new constructs to the language. Now the language is
much more, you know, much more convenient. And one of those concepts is that they change a little bit
how you assign a variable. The old ways that works, but now you can do another way. And this
other new way is what I'm going to teach you. Okay, so you declare a variable with a keyword
called cost. Okay, cost. So cost. And then you're going to say you have to give the variable a name.
It could be anything with certain exceptions for the name convention, but I won't get it to that
here. But let's make it simple. Let's say we want to store our names. I'm going to call the variable
name. And the value this variable is going to hold, you assign it with the equal operator. Okay.
Now you name this case has to be a string, right? Because it's a name. And to that, I am going to
write my name. Well, you can write your name, but I'll write my name. Okay, so let's take a look at this.
So this is going to be your name. It's a string, remember? String is like a sequence of characters.
So on the equal sign, it's an assignment operation. Okay, you're assigning
your name to a variable called name this case. Okay, the variable, you choose the name of the
variable. When you do programming, and you learn what development you learn that's always good to
name your variables to give good name to your variables. So like that, the programmers and
developers will understand what's going on right away when they see the name of the variables. So
try to name give them a very good name, don't go around naming variables with just one letter or
something very cryptic. That's a very bad practice if you're doing development in the real world.
All right. So const is a keyword. It's a new thing in ES6, the set before,
new version of JavaScript, which is going to be the de facto standard now.
Supported by most hopefully is going to be supported by most fully supported by most
major browsers very, very soon. Okay. So const, and then the name of the variable equals whatever
you want it to be. What's the value? This is called the value of this variable. Okay, now
you type enter and then you define the variable. Don't worry about this undefined here. It's the
it's actually a return value of this expression. Don't worry about it. Just ignore it for now. Okay.
So we define a variable called name that holds the value my name. Okay, so
if you want to see what's inside the variable name, since we are in the console, we can just type
name, okay, and then enter. And there you go. It's going to say, okay, the value of the variable
name is my name. Now, if you want to use this, for example, you want to do a console log,
you want to log to the console, the variable, you can use that function we learned console log,
right? And then you give an an argument is between the parentheses, you give the function an argument.
We're going to give the argument, let's say we just want to console log or name. So you can
just type the variable name there. Notice there's no strings. Okay, because this is a variable.
And the variable name here has some value. And this value here, my name is going to be
substituted here. So it's going to be actually like this. Okay. And then the console log is going to
print my name. Okay, so let's see if it really works.
Okay, so right here, this line, it print my name. Okay.
So that's
those are variables. Okay.
So let's review, let's review. What we did was define a variable called name, whose value is a string
that included your name, my name. We use the keyword const to define this variable.
Then you can console log it and just give it the name of the variable and the value of the variable
will be substituted here and then be print. Okay.
So notice there's no quotes around it because this is not a string. This is a variable. All right.
So there's a difference there. Now I want to challenge you.
I want you to define a variable. Okay. And this variable is going to be called age.
So I want you to define a variable called age, whose value is going to be your age. You don't
have to tell me, but you can do it by yourself. Okay. And I'll give a couple of seconds. And as
for the value, it could be a string, but what we're going to learn as we're going to learn later,
there's also another type of value in JavaScript. And that's a number. Yes. So you can actually type
a number and assign it to a variable. And you don't need quotes for that because that's how
you differentiate numbers from strings. A string is enclosed by quotes, but a number is not. A
number is just numbers by themselves. Okay. So I want you to try that out. Define a variable
whose name is age and value is your age as a number. Okay. Type the number, not a string.
I'll give you a couple of seconds. So how did it go? So I'm going to hear define a variable,
start with keyword const. Then I give the name of the variable I called age. And now we're going
to assign a variable, a value to this variable with the assignment operator. And I'm going to just
make something up. All right. I'm going to put 35 as a number, not a string. Notice there's no
quotes around it. So it's an actual number, another type in JavaScript. So that's it. I can see that
there's actually 35 inside that variable. And I want you to console log it. So let's console
console log age. See what we get 35. Same as previous line. Okay.
So that's it. Hope you liked it. We learned about variables. And at the end, we learned about a
new type of value. That's the number. So thank you so much for watching. Please like
video and subscribe. And until the next time, bye.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: