TL ; DR:
Variables are containers for storing values. You create a variable using the let
keyword.
This is how you create a variable named car
and initialize it with string "Toyota"
:
let car = "Toyota";
This is how you create a variable named experience
and initialize it with number 3
:
let experience = 3;
Full lesson:
Variables are containers for storing values. A piece of information / data lives in memory and we use a variable to store and descriptively label that data.
Variable creation:
We tell JavaScript to create or declare a variable by putting the keyword let
in front of the variable's name, like so:
let name;
creates a variable named name
.
Variable initialization:
We can initialize a variable to an initial value in the same line as it is created using the assignment operator (=
). This code:
let name = "AlgoCademy";
creates a new variable named name
and assigns it an initial value of "AlgoCademy".
When executing this code, JavaScript will allocate some memory, then it will store the string "AlgoCademy" in that memory and finally will attach this name
label to that memory location.
You can think of this memory location as a box. In that box, we put string "AlgoCademy". Then, we put the label name
on this box.
Accessing values in variables:
Now we can use this label anywhere in our program to access the value in that box. We can print it for example:
// We create and initialize two variables:
let name = "AlgoCademy";
let age = 10;
// We access the variables:
console.log(name);
console.log(age);
The output of this code is:
AlgoCademy
10
Undefined:
In the first piece of code, we have created the container name
. That container was empty, we didn't put any data inside it.
In other words, we have created the variable name
, but we haven't assigned any value to it yet.
And so, the output of this code:
let name;
console.log(name);
would be:
undefined
The JavaScript undefined
property indicates that a variable has not been assigned a value, or not declared at all.
We'll learn much more about this in future lessons, but we want to avoid having undefined
values in our programs as much as possible.
Important notice:
We almost never want to create a variable without initializing it! It's a bad coding practice and can cause a lot of problems.
Assignment
Follow the Coding Tutorial and let's create some variables.
Hint
Look at the examples above if you get stuck.
In this lesson, we will explore the concept of variables in JavaScript. Variables are fundamental to programming as they allow us to store and manipulate data. Understanding how to use variables effectively is crucial for writing efficient and maintainable code. Variables are used in almost every aspect of programming, from simple scripts to complex applications.
At its core, a variable is a container for storing data values. In JavaScript, we use the let
keyword to declare a variable. For example:
let car = "Toyota";
Here, we have created a variable named car
and initialized it with the string value "Toyota"
. Similarly, we can create a variable to store a number:
let experience = 3;
Understanding these basics is essential before moving on to more complex aspects of variables.
Let's delve deeper into the key concepts and techniques related to variables in JavaScript:
We declare a variable using the let
keyword followed by the variable name:
let name;
This creates a variable named name
but does not assign any value to it.
We can initialize a variable with a value at the time of declaration using the assignment operator (=
):
let name = "AlgoCademy";
This assigns the string "AlgoCademy"
to the variable name
.
Once a variable is declared and initialized, we can access its value using the variable name:
let name = "AlgoCademy";
let age = 10;
console.log(name); // Output: AlgoCademy
console.log(age); // Output: 10
Let's look at some examples to understand how variables are used in different contexts:
let userName = "John Doe";
let userAge = 25;
console.log("User Name: " + userName);
console.log("User Age: " + userAge);
In this example, we store the user's name and age in variables and then print them to the console.
let length = 10;
let width = 5;
let area = length * width;
console.log("Area of the rectangle: " + area);
Here, we use variables to store the dimensions of a rectangle and calculate its area.
When working with variables, it's important to avoid common mistakes and follow best practices:
undefined
values.As you become more comfortable with variables, you can explore advanced techniques such as:
const
for ConstantsThe const
keyword is used to declare variables that should not be reassigned:
const pi = 3.14159;
Use const
when you want to ensure that a variable's value remains constant.
let
and const
Variables declared with let
and const
are block-scoped, meaning they are only accessible within the block they are defined in:
if (true) {
let blockScoped = "I am block scoped";
console.log(blockScoped); // Output: I am block scoped
}
console.log(blockScoped); // Error: blockScoped is not defined
Let's implement a simple program that demonstrates the use of variables:
// Declare and initialize variables
let firstName = "Jane";
let lastName = "Doe";
let age = 30;
// Concatenate strings
let fullName = firstName + " " + lastName;
// Print the values
console.log("Full Name: " + fullName);
console.log("Age: " + age);
This code snippet declares and initializes variables, concatenates strings, and prints the values to the console.
Debugging and testing are essential parts of writing reliable code. Here are some tips:
console.log()
to print variable values and track the flow of your program.Writing tests helps ensure your code works as expected. Here's an example of a simple test:
function add(a, b) {
return a + b;
}
// Test the add function
console.assert(add(2, 3) === 5, "Test Failed: 2 + 3 should equal 5");
console.assert(add(-1, 1) === 0, "Test Failed: -1 + 1 should equal 0");
When solving problems related to variables, consider the following strategies:
In this lesson, we covered the basics of variables in JavaScript, including declaration, initialization, and accessing values. We also discussed common pitfalls, best practices, and advanced techniques. Mastering variables is essential for writing efficient and maintainable code. Keep practicing and exploring further applications to strengthen your understanding.
For further reading and practice, check out the following resources: