JavaScript Variables - let and const

Account Required

This free lesson is available if you sign in.
If you don't have an account, you can sign up for free.

Professional Modern JavaScript Lessons

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36

JavaScript Variables - let and const

Sign in to access this content

You must sign in to access this content.
If you don't have an account you can sign up for free!

Let

let example = 'Hello World';

console.log(example); // 'Hello World'
console.log(example); // 'Hello World'

example = 'Allow reassignment';

console.log(example); // 'Allow reassignment'

example = 1337;

console.log(example); // 1337

Const

const message = 'Hello World';

console.log(message); // Hello World

// It can never be reassigned (`=`)

Differences

  • const must be initialized
{
let withLet; // Okay
}

{
const withConst; // Error: Must be initialized
}
  • const cannot be reassigned
{
let withLet = 0;
withLet = 1; // Okay
}

{
const withConst = 0;
withConst = 1; // Error: Cannot reassign a constant
}
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

Modern JavaScript has two ways for declaring variables, and in addition to discussing the technical differences between Let versus Cons, you'll also look at the things that are common between these two ways. So let's go. The first way to declare a variable within JavaScript is with the lead keyword. We write the lead keyword followed by the variable name with an optional assignment that we want for this variable. The main advantage of using variables is that they allow you to use the same value again and again without having to type that out. So when we log out example, we don't have to type out Hello World again and again because that is a value

00:34

that is contained within the example variable. A feature offered by the lead variable is that we can assign it new value if we want to. For example, we can assign it a different string and now when we log it out, we get the new string and it doesn't even have to be a string. It can be any other value that we want. For example, it could be a number, and now when we log it out, we get this new number. A key feature of creating a variable with Let is that initialization is optional. What this means is that we can create a variable with let and not assign it any value. Now at this point, if we try to use this variable,

01:07

we get that special type that we talked about, which is undefined, which is the value that the JavaScript runtime returns. If a variable has not been initialized, of course, at any point after we have done an assignment, the variable will contain the value that we assigned. The other way to create variables within JavaScript is with the cons keyword. And as we look at the differences between a Latin cons, you will find them to be quite intuitive. Hey, we are defining a variable called a message with the cons keyword and assigning it an additional value of the Hello World string. Now of course, the advantage of using a variable is that we can use the variable without having

01:44

to type that value again and again. That value is what the variable will return. Now at this point, this looks very similar to a let keyword, so let's discuss the differences between let and Const. Now with the lead keyword, we do not have to provide an initialization because of course the lead keyword allows us to reassign that variable. At any point that we please with the const, we must provide an initial value. If we do not provide a value, the Java kept runtime will throw an error, and if uncaught, our program will exit. Now, the reason why we must provide a value when we declare a cons variable is

02:20

because of the other restriction that exists on cons. That is, once we have declared them, we cannot assign them a different value, the restriction that we must provide an initial value and that we cannot assign a different value once it has been initialized. Other differences between Let and Const, the key feature of these JavaScript variables is that they prevent city mistakes. For example, trying to use a variable before it is declared or trying to declare another variable with the same name. Let's declare a simple variable called example. Now, if you try to use this variable before it has been declared,

02:55

the JavaScript runtime will throw an error that this variable is being used Before it's been declared any portion of a code where a particular variable declaration is not valid. For example, the section before the variable declaration is what fancy JavaScript engineers might call a temporal dead zone, where temporal is basically for time. That is in a matter of time, this code is going to execute before the code to execute the variable declaration. Now, another silly mistake that is prevented by these variables is trying to declare them. Again,

03:27

this is primarily a programmer error in larger code base where we do not realize that this variable has already been declared and we should use a different name in order to not conflict with the existing code. Modern Java steel variables are ally scoped to the code block that they are declared in, which means that there are less chances of name collisions and unexpected variable values. Now, we will look at various code blocks like if LS functions fall loops in future lessons, but the fundamental thing that is common between them is the presence of parentheses.

03:58

So we can actually use parentheses by themselves to create a very simple code block. Of course, since the message variable is declared within this code block, it is available within this code block so we can safely log out its value. Now this variable will actually not exist outside of this code block, which means that if we create another code block, we can use the same variable name and provide it a different value and everything will be perfectly fine. However, outside of this code block or in another code block, this variable will not exist, and if we try to use it, the JavaScript run time will throw an error.

04:34

As you can see, modern JavaScript variables are quite well controlled to prevent silly mistakes and to recap, let versus cons, if you plan on reassigning a variable use, let otherwise use cons. Thank you for joining me and I will see you in the next one.

Professional Modern JavaScript

Professional Modern JavaScript

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36