Skip to main content

Data Types in JavaScript: A Complete Guide with Practical Examples

Learn about the different types of data in JavaScript

Learn about data types in JavaScript: primitives and objects. Learn how to use them in your code step by step with practical examples.

Primitive Data Types

JavaScript has several primitive data types that are fundamental to programming. The main primitive data types are:

Number (Number)

The data typeNumberIt represents both whole numbers and floating-point numbers (decimals).

let entero = 42; // Número entero
let decimal = 3.14; // Número decimal
let negativo = -7; // Número negativo

Text string (String)

The data typeStringIt represents a sequence of characters, such as words or phrases. Text strings can be defined using single quotes, double quotes, or backticks (literal templates).

let saludo = "Hola, mundo!"; // Comillas dobles
let nombre = "Juan"; // Comillas simples
let mensaje = `Bienvenido, ${nombre}!`; // Backticks (plantilla literal)

Boolean (Boolean)

The data typeBooleanIt has two possible values:true(true) andfalse(false). It is used to represent logical conditions.

let esVerdadero = true; // Valor booleano verdadero
let esFalso = false; // Valor booleano falso

Null (null)

The valuenullIt represents the intentional absence of any value or object. It is an assignable value.

let valorNulo = null; // Valor nulo

Indefinite (undefined)

The valueundefinedThis indicates that a variable has been declared but not assigned a value.

let valorIndefinido; // Variable sin valor asignado
console.log(valorIndefinido); // Imprime: undefined

Difference innullandundefined

It's important to understand the difference betweennullandundefined.nullis an assigned value that indicates the absence of value, whileundefinedThis means that a variable has been declared but no value has been assigned to it.

let variable1 = null; // variable1 tiene un valor nulo
let variable2; // variable2 está indefinida

Less commonly used primitive data types

BigInt (BigInt)

The data typeBigIntIt is used to represent very large integers that exceed the limit of the typeNumberIt is created by adding an "n" to the end of the number.

let numeroGrande = 9007199254740991n; // Un BigInt

Symbol (Symbol)

The data typeSymbolIt is used to create unique identifiers. Each time a symbol is created, it is unique and immutable.

let simbolo1 = Symbol("descripcion");
let simbolo2 = Symbol("descripcion");
console.log(simbolo1 === simbolo2); // Imprime: false

Non-primitive data types

In addition to primitive data types, JavaScript also has non-primitive data types, which include:

Object (Object)

The data typeObjectIt is a collection of properties, where each property is an association between a key (name) and a value. Objects are used to store structured data.

let persona = {
nombre: "Ana",
edad: 30,
esEstudiante: false,
};

Arrangement (Array)

The data typeArrayIt is an ordered collection of values, which can be of any data type, including other arrays or objects.

let numeros = [1, 2, 3, 4, 5]; // Arreglo de números
let mezclado = [1, "dos", true, null, { clave: "valor" }]; // Arreglo con diferentes tipos de datos

Summary

In summary, data types in JavaScript are fundamental for storing and manipulating information. Primitive data types includeNumber,String,Boolean,nullandundefined, while non-primitive data types includeObject and ArrayUnderstanding these data types is essential for writing effective JavaScript code.

Exercises