Data types introduction

In JavaScript, you will encounter the different types of data. Some of them are very common, and these lessons will help you build a strong foundation in all of them. There are 8 basic data types in JavaScript, which are:

  • string: represents textual data.
  • number: an integer or a floating-point number.
  • BigInt: an integer with arbitrary precision.
  • Boolean: Any of two values: true or false.
  • Symbol: data type whose instances are unique and immutable.
  • undefined: a data type whose variable is not initialized.
  • null: denotes a null value.
  • Object: key-value pairs of collection of data.

JavaScript String:

Strings are very important data types in JavaScript. They’re used to store text and are a fundamental building block of the language.

In JavaScript, strings are surrounded by quotes:

  • Single quotes: ‘Hello’
  • Double quotes: "Hello"
  • Backticks: `Hello`
//strings example
const name = 'user';
const name1 = "user1";
const result = `The names are ${name} and ${name1}`;

Single quotes and double quotes are practically the same, and you can use either of them.

Backticks are generally used when you need to include variables or expressions into a string. This is done by wrapping variables or expressions with ${variable or expression} as shown above.

You can read more about Strings and their methods through these links:

JavaScript Number:

Number represents integer and floating numbers (decimals and exponentials).

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5

JavaScript BigInt:

In JavaScript, Number type can only represent numbers less than (253 – 1) and more than -(253 – 1). However, if you need to use a larger number than that, you can use the BigInt data type.

A BigInt number is created by appending n to the end of an integer.

// BigInt value
const value1 = 900719925124740998n;

// Adding two big integers
const result1 = value1 + 1n;
console.log(result1); // "900719925124740999n"

const value2 = 900719925124740998n;

// Error! BitInt and number cannot be added
const result2 = value2 + 1; 
console.log(result2); 

Note: BigInt was introduced in the newer version of JavaScript and is not supported by many browsers including Safari.

JavaScript Boolean:

This data type represents logical entities. Boolean represents one of two values: true or false. It is easier to think of it as a yes/no switch.

const dataChecked = true;
const valueCounted = false;

Notes regarding the undefined, null and Object values

undefined: Can be thought of as a value used for unintentionally missing values, for example declared variables with no values assigned to them:

let myEmptyVariable;
/* In the next line we are accessing a variable, but forgot to assign a value to it */
console.log( myEmptyVariable ); 

// undefined

Remember, that the language specification states that:

undefined is used when a variable has not been assigned a value

You will also find undefined when trying to access Object properties that have not been created (more on this in the next lessons) and on function parameters that have not been assigned a value (also, more on this later).

null: Can be used for intentionally missing values.

Here’s how the language specification describes null:

null represents the intentional absence of any object value

Unfortunately, JavaScript (still) thinks that the typeof null is object, so be careful when you are trying to detect the type of value.

Also, here, all data types except Object are primitive data types, whereas Object is non-primitive.

The Object data type (non-primitive type) can store collections of data, whereas primitive data type can only store a single data.

We’ll go into more details on the Object data type in the following lessons.

Variables:

Variables are containers for storing data (storing data values). You can think of variables as something close to your contacts in your mobile phone. Each contact list entry contains a name, e.g. Mary. This is your variable name. And each contact list name (variable name) points to a place in the memory of your mobile phone that stores a telephone number or address, e.g. 6985252114.

Essentially, there are 4 Ways to Declare a JavaScript Variable:

  • Using var
  • Using let
  • Using const
  • Using nothing

Here are some examples:

var x = 5;
var y = 6;
var z = x + y;
let x = 5;
let y = 6;
let z = x + y;
x = 5;
y = 6;
z = x + y;

From all the examples above, you can guess:

  • x stores the value 5
  • y stores the value 6
  • z stores the value 11

Always declare JavaScript variables with var,let, or const. The var keyword is used in all JavaScript code from 1995 to 2015. The let and const keywords were added to JavaScript in 2015. If you want your code to run in older browsers, you must use var

Another example:

const price1 = 5;
const price2 = 6;
let total = price1 + price2;

The two variables price1 and price2 are declared with the const keyword. These are constant values and cannot be changed. However, the variable total is declared with the let keyword. This is a value that can be changed.

There are also some key differences between these keywords that you need to be aware of. You can learn more about them through this video: