Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Storing Data | Manipulating Data
Introduction to JavaScript
course content

Conteúdo do Curso

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
2. Manipulating Data
3. Conditional Statements

book
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:

js

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:

js

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:

123
let username; username = "John Smith"; console.log(username);
copy

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:

12
let exampleVar; console.log(exampleVar);
copy

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;:

12
let username = "John Doe"; console.log(username);
copy

Two variables cannot have the same names, otherwise it would result in an error:

12
let variableA; let variableA; // Error at this line
copy

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:

  1. The variable names cannot begin with a number;
  2. Variable names cannot contain special characters except $ and _;
  3. Variable names cannot be the same as any reserved keyword that JavaScript uses. For example, using let or const 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?

8.

question mark

What is the purpose of a variable in JavaScript?

Select the correct answer

question mark

Which of the following is the correct syntax for declaring a variable in JavaScript?

Select the correct answer

question mark

What is the default value of an uninitialized variable?

Select the correct answer

question mark

What is the difference between declaring and initializing a variable?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

question mark

Which of the following correctly declares and initializes a variable in one statement?

Select the correct answer

question mark

What will be printed to the console?

Select the correct answer

question mark

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1
Sentimos muito que algo saiu errado. O que aconteceu?
some-alt