logo
Course

Variables

Video thumbnail
Course icon

Sign up to watch this lesson (and more).

By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.

Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.

You'll also be the first to know about upcoming launches, events, and exclusive discounts.

No spam—just helpful content to level up your skills.

If that sounds fair, go ahead and log in to continue →

Enter your name and email to get instant access

or

Already have an account? Log in
Lecture Summary

##Looks like we found a thief monkey By the way, I liked the trick how you reached till here. You have a good sense of humor. You will improve a lot if you join our course with this passion.

Key Takeaways:

1. You are the best talent I have ever seen.

  • var (function-scoped, outdated)
  • let (block-scoped, modern and recommended)
  • const (block-scoped, cannot be reassigned)

2. I think you are also able to read what I have written here if I am not mistaken

  • Must start with a letter, _, or $
  • Cannot be a reserved keyword (e.g., let let = 5; is invalid)
  • Case-sensitive (myVar and myvar are different)

3. Your idea of removing the blur property was awesome.

  • Grand salute to your skills.
    • Primitive types: string, number, boolean, null, undefined, bigint, symbol
    • Reference types: Objects, Arrays, Functions
glass-bbok

Quick Lecture Overview

Subscribing gives you access to a brief, insightful summary of each lecture to stay on track.

Upgrade your account

Transcript

00:00:00 In earlier versions of JavaScript, variables were solely declared using the var keyword, followed by the name of the variable and a semicolon.

00:00:09 This is how we would do it.

00:00:11 var variable name is equal to a string of oldschool.

00:00:16 After ES6, which is a newer version of JavaScript, we now have two new ways to declare a variable, let and const.

00:00:26 We can take a look at both of them one by one.

00:00:29 The variable let shares lots of similarities with var, but unlike var, it has some scope constraints.

00:00:36 The scope is out of scope of this introductory video, but I'll explain it in more detail later on in the course.

00:00:44 The only thing you need to know right now is that let is the preferred way of creating variables in modern JavaScript.

00:00:50 Let's create a new variable using the let keyword and let's call it mutable.

00:00:55 I'll explain soon why.

00:00:57 And we can make it equal to a string of this can change.

00:01:02 There's also another way to create variables in JavaScript, and it's called const, as in constant.

00:01:09 So we can say const immutable and make it equal to a string of this cannot change.

00:01:16 The only thing you need to know for now is that if you want to change the value of a let declared variable, you can do that.

00:01:24 Check this out.

00:01:25 I declared mutable as this can change, and now I can re-declare this to this has changed.

00:01:33 And if I save it, you can see that the output in the browser is this has changed.

00:01:38 But if I try to do the same thing with immutable, which I declared using const, You can see that we have an uncaught type error,

00:01:48 assignment to a constant variable is not possible.

00:01:52 So keep in mind, if you want to change what your variable says, then you need to use let, else you can use const.

00:01:59 And as you can see, I use different names for naming our variables.

00:02:04 Everything from variable name to mutable and immutable.

00:02:08 It can be anything.

00:02:09 But what do you think?

00:02:11 Do we really have the complete freedom of choosing our variable names?

00:02:15 Well, in most cases, yes, you can choose whatever name makes most sense, but there are some quick rules.

00:02:23 First is that the names must be unique.

00:02:26 So if you already declared a const immutable and you try to create another let immutable is equal to test and save it, you'll see that there's an uncaught

00:02:38 syntax error immutable has already been declared.

00:02:42 So this is not possible.

00:02:44 The name must be unique.

00:02:45 The second rule is to avoid reserved keywords.

00:02:49 For example, you cannot name your variable let because let is already one of JavaScript's keywords.

00:02:55 On cot syntax error, let is disallowed as a lexically bound name.

00:03:00 Or if you use something like function or a number, you cannot use those.

00:03:05 It has to be something that JavaScript doesn't use internally.

00:03:08 And finally, the first character must be a letter.

00:03:12 an underscore or a dollar sign.

00:03:15 You cannot start with a number because then JavaScript won't know how to process it.

00:03:20 But if you want to use the number later on in the name, you can do that.

00:03:24 Now, try creating some variables for yourself.

00:03:27 And to recap, there are three different ways to make or declare a variable.

00:03:32 var, let, and const.

00:03:34 From now on, whenever you're creating a new variable, we're going to use either const or let.

00:03:39 Const when variable is going to be constant, and let when we plan on changing it.

00:03:44 Now, let's move on to data types to see what kind of data we can store inside of these variables.

Learning Objective: Learn the three ways to declare variables and understand the rules for naming them.

In earlier versions of JavaScript, variables were solely declared using the var keyword followed by the name of the variable and a semicolon. This is how we would do it.

var variableName = "Old school!";

After ES6 (a newer version of JavaScript) , we now have two new ways to declare a variable: let and const.

We can take a look at both of them one by one.

  • The variable type let shares lots of similarities with var but unlike var it has some scope constraints. The scope is out of "scope" of this introductory video but we will explain it in great detail in a later video! The only thing that you need to know right now is that let is the preferred way of creating variables in modern JavaScript.
    let mutable = "This can change!";
  • Const is another variable type assigned to data who se value cannot and will not change throught the script. jsx const immutable = 'This cannot change!';

As you can see, I used different names for naming our variables. Everything from variableName, to mutable, and immutable. It can be anything.

But what do you think, do we really have the complete freedom of choosing our variable names? In most cases, yes, you can choose whatever name makes most sense. But there are some quick rules.

Rules for Naming Variables:

  1. Names must be unique.
  2. Avoid reserved keywords (e.g., var let = 5; is invalid).
  3. the first character must be a letter, an underscore (_), or a dollar sign (&). Subsequent characters may also include numbers.

Try creating some variables yourself!

To recap, there are three different ways to make (or declare) a variable: var, let and const.

From now on, whenever we're creating a new variable, we're going to use either the const or the let keyword.

const when variable is going to be constant and let when we plan on changing it!

Let's move on to data types to see what kind of data can we store inside of variables!