2.1 Variables in JavaScript
JavaScript is a flexible and dynamic programming language that supports various ways to declare variables and work with data types. In this lecture, we'll explore three key ways to declare variables (var, let, const) and the basic primitive data types in JavaScript.
Variables in JavaScript
JavaScript is a dynamically typed language. Variables in it don't have a type. Any variable can be assigned any value at any time (though there are nuances).
To create a variable, you need to write a construction like this:
var name;
You can immediately assign a value to a variable:
var name = value;
But actually, there are three whole ways to create variables in JavaScript. Let's talk about them in more detail now.
The var Operator
Variables declared with var have function or global scope. They are subject to "hoisting," meaning their declaration is moved to the top of its scope when the code is executed.
Example:
console.log(x); // undefined
var x = 10;
console.log(x); // 10
In this example, the variable x is hoisted to the beginning of its scope, so the first console.log outputs undefined.
In other words, "hoisting" turns our code into this:
var x;
console.log(x); // undefined
x = 10;
console.log(x); // 10
The let Operator
Developers really wanted to change the behavior of var, but they were afraid to break millions of lines of existing code. So, they just came up with another operator that also creates variables but with slightly different behavior.
Variables declared with let have block scope. This means they are only accessible within the code block (usually in curly braces {}) in which they were declared. let is not subject to hoisting like var.
Example:
The variable y exists only inside the if block.
if (true) {
let y = 20;
console.log(y); // 20
}
console.log(y); // ReferenceError: y is not defined
The const Operator
Variables declared with const also have block scope and cannot be reassigned after they are initialized. However, this does not make objects declared via const immutable - their properties can still be modified.
In other words, you can't change the variable, but you can change the object it's referencing. Good intentions, but same old story.
Example:
In this example, the constant x cannot be reassigned, but the object obj can be changed.
const x = 30;
console.log(x); // 30
x = 40; // TypeError: Assignment to constant variable.
const obj = { name: 'Alice' };
obj.name = 'Bob'; // This is allowed
console.log(obj.name); // Bob
2.2 Primitive Data Types
JavaScript supports several primitive data types, which represent simple values that are not complex objects.
Number Type
Represents any numeric values, including integers and floating-point numbers.
Examples:
let num = 42;
let pi = 3.14;
String Type
Represents textual data: strings, characters, emojis. Anything that looks like a string fits here.
Examples:
let greeting = "Hello, world!";
let facepalm = "🤦";
let char = 'x';
Boolean Type
Represents logical values true or false.
let isTrue = true;
let isFalse = false;
Null Type
Represents intentional absence of a value.
let empty = null;
If a type consists of a single value that means absence, then maybe it's more unique as a value than as a type?
Undefined Type
Represents a variable that has been declared but not initialized.
let notAssigned;
console.log(notAssigned); // undefined
2.3 Comparison of var, let, and const
Let's do a final comparison of our three ways to create variables:
| Characteristic | var | let | const |
|---|---|---|---|
| Scope | Function or global | Block | Block |
| Hoisting | Yes, but initialization does not hoist | No | No |
| Reassignment | Yes | Yes | No |
| Initialization | Optional | Optional | Mandatory |
JavaScript provides several ways to declare variables, each with its peculiarities and applicability depending on the context. var is used less frequently due to its hoisting quirks and global scope. let and const are preferred in modern JavaScript because of their block scope and reassignment control.
Primitive data types allow working with basic data structures, providing essential functionality needed to create complex applications. And it's the most popular language in the world! :)
GO TO FULL VERSION