0
0
Javascriptprogramming~20 mins

Why variables are needed in Javascript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do we use variables in programming?

Imagine you want to store your friend's name and use it later in your program. Why is it helpful to use a variable for this?

AVariables make the program run faster by skipping steps.
BVariables are only used to make the code look colorful.
CVariables automatically fix mistakes in the code.
DVariables let us store and reuse information easily without rewriting it.
Attempts:
2 left
💡 Hint

Think about how you remember things in real life to use later.

Predict Output
intermediate
2:00remaining
What is the output of this code using variables?

Look at this JavaScript code. What will it print?

Javascript
let name = 'Alice';
let greeting = 'Hello, ' + name + '!';
console.log(greeting);
AHello, name!
BHello, Alice!
Cgreeting
Dundefined
Attempts:
2 left
💡 Hint

Variables hold values that can be combined to make new messages.

Predict Output
advanced
2:00remaining
What is the value of x after running this code?

Consider this JavaScript code. What is the final value of x?

Javascript
let a = 5;
let b = 3;
let x = a + b;
a = 10;
x = x + a;
A8
B15
C18
D13
Attempts:
2 left
💡 Hint

Remember that changing a after calculating x does not change the old value of x unless you update it.

🔧 Debug
advanced
2:00remaining
Which option causes an error due to variable misuse?

Which code snippet will cause an error because the variable is used before it is declared?

A
console.log(score);
let score = 10;
B
let score = 10;
score = score + 5;
console.log(score);
C
let score;
score = 10;
console.log(score);
D
let score = 10;
console.log(score);
Attempts:
2 left
💡 Hint

Think about when variables are ready to be used in JavaScript.

🧠 Conceptual
expert
2:00remaining
Why is using variables better than repeating values?

Imagine you write a program that uses the number 7 many times. Why is it better to store 7 in a variable and use the variable instead of writing 7 everywhere?

AIt makes the program easier to update and understand.
BIt makes the program run slower because of extra steps.
CIt hides the number so no one can see it.
DIt automatically changes the number to 10.
Attempts:
2 left
💡 Hint

Think about how changing one place is easier than many places.