The power of variables


Now that we've learned about variables, let's see why they are so important in our programmers' life:

Consider this simple program which prints a story:

console.log("My name is John and I am 40 years old");
console.log("Coding at age 40 is really fun");
console.log("My son is named after me, John");
console.log("I'd love him to be as cool as me at age 40");
console.log("It's so funny when my wife calls John we both respond");

Changing the story:

This is a perfectly working program. But what if I come back to it and want to change the character's name to Andy and his age to 32?

Well, I'm going to have to go through the entire program and manually change the name John to the name Andy and the age 40 to the age 32:

console.log("My name is Andy and I am 32 years old");
console.log("Coding at age 32 is really fun");
console.log("My son is named after me, Andy");
console.log("I'd love him to be as cool as me at age 32");
console.log("It's so funny when my wife calls Andy, we both respond");

This is not horrible as I had to go through only 5 lines of code. But what if my story consisted of hundreds of lines?

It's a disaster. Going through the whole code and manually changing the name and age is boring. But more importantly, it's super easy to make mistakes.


The story with variables:

This is where variables shine. If I used a variable to store the name of the character and another variable to store his age when I first wrote this code, it would have looked like this:

let name = "John";
let age = 40;

console.log("My name is " + name + " and I am " + age + " years old");
console.log("Coding at age " + age + " is really fun");
console.log("My son is named after me, " + name);
console.log("I'd love him to be as cool as me at age " + age);
console.log("It's so funny when my wife calls " + name + " we both respond");

Changing the story with variables:

Now whenever I want to change the character's name and age, I only have to change two lines of code, instead of possibly hundreds.

If I want my character to be named Andy and aged 32, I initialize the variables name with Andy and age with 32 and leave the rest of the code untouched:

let name = "Andy";
let age = 32;

console.log("My name is " + name + " and I am " + age + " years old");
console.log("Coding at age " + age + " is really fun");
console.log("My son is named after me, " + name);
console.log("I'd love him to be as cool as me at age " + age);
console.log("It's so funny when my wife calls " + name + " we both respond");

Assignment
Follow the Coding Tutorial and let's witness the power of variables!


Hint
Look at the examples above if you get stuck.


Introduction

In this lesson, we will explore the power of variables in programming. Variables are fundamental building blocks in any programming language, allowing us to store and manipulate data efficiently. Understanding how to use variables effectively can make your code more readable, maintainable, and scalable.

Variables are particularly useful in scenarios where you need to reuse values, such as user inputs, configuration settings, or any data that might change over time. By using variables, you can avoid hardcoding values and make your code more flexible and easier to update.

Understanding the Basics

At its core, a variable is a named storage location in memory that holds a value. You can think of it as a container that can store different types of data, such as numbers, strings, or objects. In JavaScript, you can declare a variable using the let, const, or var keywords.

Here is a simple example:

let name = "John";
let age = 40;

In this example, we declare two variables: name and age. The name variable stores the string "John", and the age variable stores the number 40. Understanding these basics is crucial before moving on to more complex aspects of variables.

Main Concepts

Variables allow us to store and manipulate data dynamically. Here are some key concepts and techniques related to variables:

Let's see how to apply these concepts with an example:

let name = "John"; // Declaration and assignment
let age = 40; // Declaration and assignment

console.log("My name is " + name + " and I am " + age + " years old"); // Using variables

name = "Andy"; // Reassignment
age = 32; // Reassignment

console.log("My name is " + name + " and I am " + age + " years old"); // Using reassigned variables

In this example, we declare and assign values to the name and age variables. We then reassign new values to these variables and use them in the console.log statements.

Examples and Use Cases

Let's explore some examples and use cases where variables are beneficial:

Example 1: User Input

let userName = prompt("Enter your name:");
let userAge = prompt("Enter your age:");

console.log("Hello, " + userName + "! You are " + userAge + " years old.");

In this example, we use variables to store user input from the prompt function. This allows us to reuse the input values in the console.log statement.

Example 2: Configuration Settings

const apiUrl = "https://api.example.com";
const apiKey = "123456";

function fetchData() {
  fetch(apiUrl + "/data?api_key=" + apiKey)
    .then(response => response.json())
    .then(data => console.log(data));
}

fetchData();

In this example, we use variables to store configuration settings such as the API URL and API key. This makes it easy to update the settings without changing the function logic.

Common Pitfalls and Best Practices

When working with variables, it's important to avoid common mistakes and follow best practices:

Advanced Techniques

Once you are comfortable with the basics, you can explore advanced techniques such as:

Here is an example of using template literals:

let name = "John";
let age = 40;

console.log(`My name is ${name} and I am ${age} years old`);

Code Implementation

Let's implement the story example using variables:

let name = "John";
let age = 40;

console.log("My name is " + name + " and I am " + age + " years old");
console.log("Coding at age " + age + " is really fun");
console.log("My son is named after me, " + name);
console.log("I'd love him to be as cool as me at age " + age);
console.log("It's so funny when my wife calls " + name + " we both respond");

// Changing the story
name = "Andy";
age = 32;

console.log("My name is " + name + " and I am " + age + " years old");
console.log("Coding at age " + age + " is really fun");
console.log("My son is named after me, " + name);
console.log("I'd love him to be as cool as me at age " + age);
console.log("It's so funny when my wife calls " + name + " we both respond");

Debugging and Testing

When working with variables, debugging and testing are crucial. Here are some tips:

Here is an example of a simple test case:

function greet(name) {
  return "Hello, " + name + "!";
}

// Test case
console.assert(greet("John") === "Hello, John!", "Test failed: greet('John')");
console.assert(greet("Andy") === "Hello, Andy!", "Test failed: greet('Andy')");

Thinking and Problem-Solving Tips

When solving problems related to variables, consider the following strategies:

Conclusion

In this lesson, we explored the power of variables in programming. We discussed their significance, fundamental concepts, and various use cases. By mastering variables, you can write more flexible, maintainable, and efficient code. Keep practicing and exploring further applications to enhance your programming skills.

Additional Resources

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