Storing Data
Variables are containers of data in a computer's memory. The general syntax of creating a new variable is let variableName
.
For example, the following code declares a new variable called username
:
let username;
This is known as a Variable Declaration Statement.
We can assign a value to a variable using the syntax variableName = data
, where data can be some text, number, or any other valid data type.
For example:
let username;
username = "John Smith";
The statement in which we assign some value to a variable is known as an Assignment Statement.
We can simply use the variable name for accessing data from it:
123let username; username = "John Smith"; console.log(username);
In the above code, the term username
was passed as an argument to the console.log
statement. This simply fetches whatever is inside the username
and passes it into the console.log
, which in turn displays the data on the screen.
If a variable is not assigned any value, it holds the value undefined
by default:
12let exampleVar; console.log(exampleVar);
Assigning a value to a variable for the first time is known as Initialization.
There is a shorter syntax for declaring and initializing a variable at the same time: let variableName = data;
:
12let username = "John Doe"; console.log(username);
Two variables cannot have the same names, otherwise it would result in an error:
12let variableA; let variableA; // Error at this line
However, it's also important to note that the variable names are case-sensitive, so variableA
and VariableA
are considered as two different names.
Furthermore, there are some naming rules for the variable which we need to follow:
- The variable names cannot begin with a number;
- Variable names cannot contain special characters except
$
and_
; - Variable names cannot be the same as any reserved keyword that JavaScript uses. For example, using
let
orconst
as a variable name is not allowed.
1. What is the purpose of a variable in JavaScript?
2. Which of the following is the correct syntax for declaring a variable in JavaScript?
3. What is the default value of an uninitialized variable?
4. What is the difference between declaring and initializing a variable?
5. What will be the output of the following code?
6. Which of the following correctly declares and initializes a variable in one statement?
7. What will be printed to the console?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 1.33
Storing Data
Swipe to show menu
Variables are containers of data in a computer's memory. The general syntax of creating a new variable is let variableName
.
For example, the following code declares a new variable called username
:
let username;
This is known as a Variable Declaration Statement.
We can assign a value to a variable using the syntax variableName = data
, where data can be some text, number, or any other valid data type.
For example:
let username;
username = "John Smith";
The statement in which we assign some value to a variable is known as an Assignment Statement.
We can simply use the variable name for accessing data from it:
123let username; username = "John Smith"; console.log(username);
In the above code, the term username
was passed as an argument to the console.log
statement. This simply fetches whatever is inside the username
and passes it into the console.log
, which in turn displays the data on the screen.
If a variable is not assigned any value, it holds the value undefined
by default:
12let exampleVar; console.log(exampleVar);
Assigning a value to a variable for the first time is known as Initialization.
There is a shorter syntax for declaring and initializing a variable at the same time: let variableName = data;
:
12let username = "John Doe"; console.log(username);
Two variables cannot have the same names, otherwise it would result in an error:
12let variableA; let variableA; // Error at this line
However, it's also important to note that the variable names are case-sensitive, so variableA
and VariableA
are considered as two different names.
Furthermore, there are some naming rules for the variable which we need to follow:
- The variable names cannot begin with a number;
- Variable names cannot contain special characters except
$
and_
; - Variable names cannot be the same as any reserved keyword that JavaScript uses. For example, using
let
orconst
as a variable name is not allowed.
1. What is the purpose of a variable in JavaScript?
2. Which of the following is the correct syntax for declaring a variable in JavaScript?
3. What is the default value of an uninitialized variable?
4. What is the difference between declaring and initializing a variable?
5. What will be the output of the following code?
6. Which of the following correctly declares and initializes a variable in one statement?
7. What will be printed to the console?
Thanks for your feedback!