Understanding Variables in JavaScript
Variables let you store and reuse values in your programs.
A variable is like a container that can hold numbers, text, or more complex data.
Defining Variables
Use the let keyword to create a variable:
let variableName;
Variable names should follow camelCase (first word lowercase, next words capitalized).
A newly created variable has the value undefined, meaning no value has been assigned yet.
You can assign a value later:
let x;
x = 5;
Or assign it immediately:
let x = 13;
Using Variables
Variables help avoid repeating the same value in your code:
1234let word = "VariableText"; // The variable value is the `"VariableText"` console.log("ValueText"); // Print the `"ValueText"` console.log(word); // Print the variable value
You can reassign values anytime:
123456let numb; numb = 100; console.log(numb); numb = 100 + 20; console.log(numb);
Why Variables Matter
Without variables, you would have to rewrite the same value many times and fix every occurrence if there's a typo.
Example with repeated text:
1234console.log("Hello Word!"); console.log("I love this Word!"); console.log("I need to live in this Word!"); console.log("This Word is the best!");
Using a variable makes fixes easier:
123456let x = "Word"; console.log("Hello " + x + "!"); console.log("I love this " + x + "!"); console.log("I need to live in this " + x + "!"); console.log("This " + x + " is the best!");
Change "Word" to "World" once, and all lines update.
This uses string concatenation, which youβll learn next.
Note
In the example above, we used string concatenation. We will delve into string concatenation in the next section.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain more about string concatenation?
What other types of values can variables store?
Can you show more examples of using variables?
Awesome!
Completion rate improved to 2.5
Understanding Variables in JavaScript
Swipe to show menu
Variables let you store and reuse values in your programs.
A variable is like a container that can hold numbers, text, or more complex data.
Defining Variables
Use the let keyword to create a variable:
let variableName;
Variable names should follow camelCase (first word lowercase, next words capitalized).
A newly created variable has the value undefined, meaning no value has been assigned yet.
You can assign a value later:
let x;
x = 5;
Or assign it immediately:
let x = 13;
Using Variables
Variables help avoid repeating the same value in your code:
1234let word = "VariableText"; // The variable value is the `"VariableText"` console.log("ValueText"); // Print the `"ValueText"` console.log(word); // Print the variable value
You can reassign values anytime:
123456let numb; numb = 100; console.log(numb); numb = 100 + 20; console.log(numb);
Why Variables Matter
Without variables, you would have to rewrite the same value many times and fix every occurrence if there's a typo.
Example with repeated text:
1234console.log("Hello Word!"); console.log("I love this Word!"); console.log("I need to live in this Word!"); console.log("This Word is the best!");
Using a variable makes fixes easier:
123456let x = "Word"; console.log("Hello " + x + "!"); console.log("I love this " + x + "!"); console.log("I need to live in this " + x + "!"); console.log("This " + x + " is the best!");
Change "Word" to "World" once, and all lines update.
This uses string concatenation, which youβll learn next.
Note
In the example above, we used string concatenation. We will delve into string concatenation in the next section.
Thanks for your feedback!