Free Ebook cover How to use Javascript in website development

How to use Javascript in website development

5

(2)

37 pages

Variables and Data Types in Javascript

Capítulo 3

Estimated reading time: 2 minutes

Audio Icon

Listen in audio

0:00 / 0:00

One of the main features of Javascript is its ability to work with variables and data types. Variables are used to store values ​​that can change during code execution, while data types define the type of information being stored.

In Javascript, variables are declared using the "var" keyword, followed by the variable name and, optionally, the initial value. For example:

var name = "John";
var age = 30;
var height;

In the example above, the variable "name" receives the value "John", the variable "age" receives the value 30 and the variable "height" is declared with no initial value.

The most common data types in Javascript are:

  • String: used to store text such as names and phrases. Strings are delimited by single or double quotes. Example: "Hello world!"
  • Number: used to store numerical values, such as integers and decimals. Example: 10 or 3.14
  • Boolean: used to store true or false values. Example: true or false
  • Array: used to store a list of values, which can be of different types. Example: [1, "John", true]
  • Object: used to store a set of properties and their values. Example: {name: "John", age: 30, height: 1.75}
  • Undefined: used to indicate that a variable has no initial value. Example: var height;
  • Null: used to indicate that a variable has no value. Example: var name = null;

In addition to these data types, Javascript also has the "function" type, used to store functions that can be executed in different parts of the code.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

To find out the data type of a variable in Javascript, we can use the "typeof" operator. For example:

var name = "John";
var age = 30;
console.log(typeof name); // string
console.log(typeof age); // number

With the use of variables and data types, it is possible to create more dynamic and flexible codes in Javascript, allowing the manipulation of information in a more efficient and organized way.

Now answer the exercise about the content:

_What is the data type used to store true or false values ​​in Javascript?

You are right! Congratulations, now go to the next page

You missed! Try again.

The data type used to store true or false values in Javascript is Boolean. This is explicitly mentioned in the text as one of the common data types in Javascript.

Next chapter

Operators in Javascript

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.