- Overview
- Transcript
2.1 Variables
In this lesson we’ll look at one of the most fundamental units of a JavaScript program—the humble variable. We’ll see the different ways that a variable can be declared and then assigned a value, and some other general bits of information about variables that you should probably be aware of.
Syntax
var
let
const
1.Introduction2 lessons, 07:42
1.1Introduction02:12
1.2Setup05:30
2.Language Fundamentals8 lessons, 1:00:53
2.1Variables06:33
2.2Data Types11:28
2.3Arithmetic, Assignment, and Comparison Operators10:24
2.4Unary, Logical, Comma, and Spread Operators09:02
2.5Operator Precedence03:50
2.6Reserved Words04:17
2.7Strict Mode04:34
2.8Functions10:45
3.Data Structures5 lessons, 22:52
3.1Arrays04:29
3.2Objects04:30
3.3Sets04:57
3.4Maps04:21
3.5Weak Maps and Weak Sets04:35
4.Controlling Program Execution7 lessons, 37:06
4.1Conditionals07:49
4.2Switch Statements04:41
4.3The For Loop06:39
4.4The `for .. in` Loop05:17
4.5The `for .. of` Loop04:02
4.6Iterators05:03
4.7While Loops03:35
5.Using JavaScript13 lessons, 1:44:36
5.1Working With Strings09:32
5.2Template Literals05:46
5.3Working With Numbers06:57
5.4Working With Arrays12:53
5.5Iterating and Transforming Arrays07:33
5.6Working With the Object Type13:55
5.7Object Literal Extensions06:45
5.8Working With Object Instances06:45
5.9Getters and Setters05:00
5.10Custom Objects11:28
5.11The `Math` API04:54
5.12Working With Dates and Times08:10
5.13The `Array` Constructor04:58
6.Functions8 lessons, 56:07
6.1The `this` Object06:15
6.2Working With Functions10:11
6.3Scope07:37
6.4Arrow Functions06:59
6.5Generator Functions08:13
6.6Closures05:00
6.7Prototypes06:26
6.8Default and Rest Parameters05:26
7.Miscellaneous6 lessons, 52:39
7.1Destructuring Assignments08:09
7.2AJAX08:30
7.3Regular Expressions10:51
7.4More About Regular Expressions08:38
7.5Classes06:48
7.6ES Modules09:43
8.Working With the DOM6 lessons, 37:39
8.1Selecting HTML Elements05:02
8.2Manipulating HTML Elements07:40
8.3DOM Traversal05:25
8.4Adding and Removing Elements04:45
8.5Creating Elements and Other Nodes04:39
8.6DOM Events10:08
9.Web APIs4 lessons, 17:41
9.1The Selector API03:03
9.2Geolocation05:29
9.3Web Storage05:24
9.4Web Workers03:45
10.Asynchronous JavaScript5 lessons, 26:23
10.1Promises09:52
10.2Promise Chaining05:11
10.3The async Keyword03:21
10.4The await Keyword04:04
10.5More About async and await03:55
11.Conclusion1 lesson, 00:43
11.1Conclusion00:43
2.1 Variables
Hi folks, in this lesson, we're going to start with one of the most fundamental concepts of JavaScript, variables. A variable is a named container for a value. Once we've declared a variable, we can then use the name or identifier of the variable in other places in our code to access the value that it holds. In JavaScript, there are three different ways to declare variables, the var keyword ,the let keyword, and the const keyword. Let's look at each of them in turn, starting with var. To declare a variable with the var keyword, we just need to provide a name for the variable. So this statement creates an empty variable with the identifier foo. In this case, the variable is only declared, it's not initialized or assigned a value. So it is automatically given the special value undefined. We'll look at undefined in more detail in the next lesson. There are some rules around the characters that we can use for a variable name in JavaScript. Technically, we can use any Unicode character in an identifier, but the variable name must start with either an underscore, the dollar sign, or a letter. Numbers can also be used in variable names, but not right at the start. Space characters cannot be used at all. If we need to declare multiple variables, we can use a single var statement with different variables separated by commas. In this case, three variables are created with the identifiers foo, bar, and baz. As well as declaring a variable, we can at the same time initialize it by giving it a value. In this case, we assign the value 42 to foo using the assignment operator, which is the equal sign. 42 is a number, but notice that we didn't need to tell JavaScript that the variable would contain a number, we just assigned it. JavaScript is known as a weakly or dynamically typed language. What this means is that unlike many other programming languages, when a variable is declared in JavaScript, we don't have to specify what type of value it will store. This is great because it means that any variable can hold any type of data. We can store one type of data, say, a string, and then later on, we can overwrite the string value with a number, for example. The downside to this is that sometimes we don't know for sure what type of data or variable it's holding. So we'll often need to check before trying to do something with the value. If we want to overwrite the foo variable later on, we can just assign a new value to it. This time, we don't need to use the var keyword. One thing to watch out for with the var keyword is that it can create variables in the global scope. JavaScript has three different scopes, global scope, function scope, and block scope. And we'll be looking at scope in much detail later in the course. If a variable is defined in the global scope, it can be accessed by any other JavaScript that is loaded onto the page, even within other, separate JavaScript files. This can be useful in the case of libraries or frameworks. But it can also be dangerous because if other code can access it, other code can also change it. And this can make your program break in ways that are extremely hard to debug. Global scope should be avoided wherever possible, which thankfully is most of the time. Function scope means that a variable is only visible within the function it is declared in. When we declare a variable inside a function, the variable only exists in that function's scope, and cannot be accessed by code outside of the function. Again, don't worry too much about this for now. We'll come back and look at functions and scope in much more detail soon enough. Block scope means that a variable is only visible within the block it is declared in. A block in JavaScript is generally any number of expressions, enclosed within curly braces. Block scope is a common feature of many programming languages, but it's only recently been added to JavaScript in the last few years. So the var keyword that we've looked at so far only works with global or function scopes. If we use the var keyword inside a block, the variable will still be accessible outside of the block. If we want to enable block scope for a variable, we need to declare it using the let keyword. Syntactically, let is very similar to var, it just differs in terms of scope. With let, if we declare a variable inside a block, the variable is only visible inside that block. We haven't covered blocks yet, we will come to that later in the course when we get to section four. For now, just remember that for block scope, we need to use let. The final way of declaring a variable in JavaScript is with the const keyword. The const keyword is similar to let in that it can make use of block scope, but it has one important difference. The const keyword creates a constant value that cannot be changed through reassignment. So we can't, for example, do something like this. This will actually throw an error because we're trying to reassign the value of baz to 43. But it was declared using the const keyword. So in this lesson, we learned the three different ways that we can declare a variable, the var, let, and const keywords. And we saw the differences in behavior between them. We saw that the var key word only works with global or function scope. But that let and const are able to make use of block scope when used inside a block. In modern JavaScript, it is common to most often use let and const rather than var because of the global scope issues. In the next lesson, we will learn another fundamental aspect of JavaScript, the different data types that are available. Thanks for watching.