Data types are the classifications we give to the different kinds of data that we use in programming. In JavaScript, there are seven fundamental data types:

  1. Number: Any number, including numbers with decimals: 48151623.42.
  2. String: Any grouping of characters on your keyboard (letters, numbers, spaces, symbols, etc.) surrounded by single quotes: ' ... ' or double quotes " ... ". Though we prefer single quotes. Some people like to think of string as a fancy word for text.
  3. Boolean: This data type only has two possible values— either true or false (without quotes). It’s helpful to think of booleans as on and off switches or as the answers to a “yes” or “no” question.
  4. Null: This data type represents the intentional absence of a value, and is represented by the keyword null (without quotes).
  5. Undefined: This data type is denoted by the keyword undefined (without quotes). It also represents the absence of a value though it has a different use than null.
  6. Symbol: A newer feature to the language, symbols are unique identifiers, useful in more complex coding. No need to worry about these for now.
  7. Object: Collections of related data.

The first 6 of those types are considered primitive data types. They are the most basic data types in the language. Objects are more complex, and you’ll learn much more about them as you progress through JavaScript. At first, seven types may not seem like that many, but soon you’ll observe the world opens with possibilities once you start leveraging each one. As you learn more about objects, you’ll be able to create complex collections of data.

1. Number

let n = 111;
n = 23.353;

The number type represents both integer and floating point numbers.

2. String

A string in JavaScript must be surrounded by quotes.

let str = "Hello World";
let str2 = 'Second Strings are also good';
let phrase = `can embed another ${str}`;

In JavaScript, there are 3 types of quotes.

  1. Double quotes: "Hello".
  2. Single quotes: 'Hello'.
  3. Backticks: `Hello`.

3. Boolean (logical type)

The boolean type has only two values: true and false.

This type is commonly used to store yes/no values: true means “yes, correct”, and false means “no, incorrect”.

For instance:

let nameFieldChecked = true; // yes, name field is checked
let ageFieldChecked = false; // no, age field is not checked

Boolean values also come as a result of comparisons:

let isGreaterno = 4 > 1;

alert( isGreaterno ); // true (the comparison result is "yes")

4. The “null” value

It forms a separate type of its own which contains only the null value:

let value1 = null;

In JavaScript, null is not a “reference to a non-existing object” or a “null pointer” like in some other languages.

It’s just a special value which represents “nothing”, “empty” or “value unknown”.

The code above states that value1 is unknown or empty for some reason.

5. The “undefined” value

The special value undefined also stands apart. It makes a type of its own, just like null.

The meaning of undefined is “value is not assigned”.

If a variable is declared, but not assigned, then its value is undefined:

let x;

alert(x); // shows "undefined"

Normally, we use null to assign an “empty” or “unknown” value to a variable, and we use undefined for checking whether if a variable has been assigned or not.

6. Objects and Symbols

The object type is special.

All other types are called “primitive” because their values can contain only a single thing. (be it a string or a number or whatever).

The symbol type is used to create unique identifiers for objects. We mention it here for completeness, but we’ll study it after objects.

Learn More about Objects:

7. The typeof operator

The typeof operator returns the type of the argument. It’s used when we want to process values of different data types or just want to do a check.

It supports two forms of syntax:

  1. As an operator: typeof x.
  2. As a function: typeof(x).

In other words, it works with parentheses or without them. The result is the same.

The call to typeof x returns a string with the type name:

typeof undefined // "undefined"

typeof 0 // "number"

typeof 10n // "bigint"

typeof true // "boolean"

typeof "foo" // "string"

typeof Symbol("id") // "symbol"

typeof Math // "object"  (1)

typeof null // "object"  (2)

typeof alert // "function"  (3)

Leave a Reply

Your email address will not be published. Required fields are marked *