Empty Variables
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 9 of 77.
In JavaScript, it's possible to declare variables without assigning them a specific value. This can be useful when you know you'll use a variable later in your code, but you don't yet know its initial value.
To declare an empty variable in JavaScript, you can use the let keyword followed by the variable name, without assigning any value:
let myVariable;This creates a variable named myVariable but doesn't assign any value to it. The variable is said to be uninitialized.
When you try to access an uninitialized variable, it will have a special value called undefined:
console.log(myVariable); // Output: undefinedYou can assign a value to an uninitialized variable later in your code using the assignment operator =:
myVariable = 10;
console.log(myVariable); // Output: 10Challenge
BeginnerDeclare two empty variables named a and b.
Cheat sheet
To declare an empty variable in JavaScript, use the let keyword followed by the variable name:
let myVariable;Uninitialized variables have the value undefined:
console.log(myVariable); // Output: undefinedYou can assign a value to an uninitialized variable later using the assignment operator =:
myVariable = 10;
console.log(myVariable); // Output: 10Try it yourself
// Type your code below
// Don't change the line below
console.log(`a = ${a}, b = ${b}`)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False