JavaScript data types: Intro

JavaScript variables may be numbers, strings, objects, booleans, and more.




In the JavaScript variable guide, the variables were all given number type values.

But in JavaScript, there are 7 different data types. 5 out of the 6 are primitive types.

Dynamic typing

It's important to note that JavaScript is a loosely typed or dynamic typed language. That means JavaScript variables can be assigned, and re-assigned to values of all types.

let price = 102; // price is a number
price = '105'; // price is now a string
price = false; // price is now a boolean

Data type: Strings

Strings, also known as text, is a series of characters.

To declare a string variable type the value must be wrapped around single, double or backtick quotes.

let person1 = "John Doe"; // Double quotes
let person2 = 'Sally Sue'; // Single quotes
let person3 = `Spot`; // Backtick quotes

You may use quotes inside of a string, as long they don't match the initial quotes surrounding the string value.

const greeting1 = "Hi there, I'm Ruben"; // Single quote inside double quotes
const greeting2 = 'She said, "you are kidding me right?"'; // Double quotes inside single quotes

Backticks were introduce with ES6. These special types of quotes let you interpolate. Interpolate means to embed a variable inside a string.

const age = 22;

// Old concatenate method
console.log('I am ' + age + ' years old.'); // I am 22 years old.

// New interpolate method
console.log(`I'm ${age} years old.`); // I'm 22 years old.

The old method to embed a variable inside a string was called, concatenating. This method requires you to use the + operator.

You can avoid that mess with backtick quotes.

To learn more about Strings check out this article: Data type - Strings

Data type: Numbers

Number type values may be negative or positive numbers. Number type variables may also have a decimal.

const num1 = -100;
const num2 = 50;
const num3 = 13.50;

Data type: Booleans

Booleans variable types may only be true orĀ false.

They're usually used for conditional statements.

const animalType = 'dog';
let isHappy = false;

// Change 'isHappy' value to true if
// animal type is dog, and if I'm not happy already.
if ('dog' === animalType && false === isHappy) {
  isHappy = true;

console.log(isHappy); // I'm happy!

Data type: Undefined

The undefined data type, means a variable has not been assigned a value.

let name; // Declares variable without value.

name = 'Johnny'; // Change variable to string

name = undefined; // Variable is now undefined again.

In JavaScript, you may declare a variable without a value assigned to it. It will make the default value to be undefined.

You may also directly give it the undefined value as shown above.

You cannot do the same declaration style to const variable types.const require a declaritive value assigned to it.

const foo; // This will break your JavaScript

const bar = undefined; // This works!

Data type: Null

In JavaScript null means "nothing".

let person = null;

Do you want more JavaScript articles?

Hey, here at Linguine Code, we want to teach you everything we know about JavaScript. Our only question is, are you in?