...

Declaration and Assignment

Earlier, we learned about creating variables and assigning them values in a single line:

let str = "Hello, World!";

In this exercise, we will break that into two separate lines: declaration and assignment.

// declare a variable named str (declaration)
let str; 

// assign a value to the variable (assignment)
str = "Hello, World!";

Why?

This two-step process may seem unnecessary at first, but as we will see in future lessons, it is a very powerful tool for programs that need to build logic based on conditions.

Run the code in this exercise to see separate declaration and assignment in action.

Loading...

Output (Console)