Accessing Variables in JavaScript


TL ; DR:

  • After creating a variable, you can access the value in the variable, to print it for example:

    // Creation:
    let car = "Toyota";
    
    // Access:
    console.log(car); // Prints "Toyota"




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.


Introduction

In this lesson, we will explore the concept of variables in JavaScript, focusing on how to create, initialize, and access them. Variables are fundamental in programming as they allow us to store and manipulate data. Understanding how to work with variables is crucial for any aspiring developer, as they are used in virtually every program.

Understanding the Basics

Variables are essentially containers for storing data values. When we create a variable, we allocate a space in memory to hold a value, and we give that space a name (the variable name). This allows us to refer to the stored value by its name, making our code more readable and easier to manage.

For example, consider the following code:

let car = "Toyota";
console.log(car); // Prints "Toyota"

Here, we create a variable named car and assign it the value "Toyota". We then access the value stored in car using console.log, which prints "Toyota" to the console.

Main Concepts

Let's break down the key concepts involved in working with variables:

Examples and Use Cases

Let's look at some examples to understand how variables are used in different contexts:

// Example 1: Storing a string value
let greeting = "Hello, World!";
console.log(greeting); // Prints "Hello, World!"

// Example 2: Storing a number value
let age = 25;
console.log(age); // Prints 25

// Example 3: Storing a boolean value
let isStudent = true;
console.log(isStudent); // Prints true

In these examples, we store different types of values (string, number, and boolean) in variables and print them to the console.

Common Pitfalls and Best Practices

When working with variables, there are some common mistakes to avoid and best practices to follow:

Advanced Techniques

As you become more comfortable with variables, you can explore advanced techniques such as:

Code Implementation

Here is a complete example demonstrating the creation, initialization, and access of variables in JavaScript:

// Creating and initializing variables
let name = "AlgoCademy";
let age = 10;

// Accessing and printing variable values
console.log(name); // Prints "AlgoCademy"
console.log(age); // Prints 10

// Example of an uninitialized variable
let uninitializedVar;
console.log(uninitializedVar); // Prints "undefined"

Debugging and Testing

When working with variables, it's important to test and debug your code to ensure it behaves as expected. Here are some tips:

Thinking and Problem-Solving Tips

When working with variables, consider the following strategies:

Conclusion

In this lesson, we covered the basics of creating, initializing, and accessing variables in JavaScript. We also discussed common pitfalls, best practices, and advanced techniques. Mastering variables is essential for any programmer, as they are a fundamental part of writing efficient and maintainable code. Keep practicing and exploring further applications to deepen your understanding.

Additional Resources

For further reading and practice, check out the following resources: