JavaScript #4 ~ Data Types

Variables can store various types of data in their container. Before going into it any further, it is important to first address the categories within types of data. A single value can either be categorized as an object or a primitive value. For this post, I will only talk about primitive values and explain object values in a future post (as it is easier to follow-through that way 🙂).

7 Primitive Data Types

1) Number: Any number value that is shown as either a decimal or an integer.

let age = 23;
let decimal = 1.01;

2) String: Sequence of characters used for text.

let firstName = 'John';
let lastName = 'Smith';

3) Boolean: Logical type that can only be true or false which is used for taking decisions.

let isTrue = true;
let isFalse = false;

4) Undefined: a value taken by a variable that is not yet defined ('empty value').

let container;

5) Null: similar to undefined, null also means 'empty value', but done intentionally.

let container = null;

6) Symbol (ES2015): a value that is unique and cannot be changed.

7) BigInt (ES2020): larger integers than the Number type can hold.

Typeof

The typeof operator is used to determine the type of a particular data in string format. The typeof operator is useful because it is an easy way to check the type of a variable in a code. This is important because JavaScript is a is a dynamically typed language (more on this below). This means that you aren’t required to assign types to variables when you create them.

let word = 'hello'
typeof word; // 'string'
let num = '1';
typeof num; // 'number'
let isTrue = true;
typeof isTrue; // 'boolean'

More on Data Types

Although there are many types of data that can be used in JavaScript, developers only mostly use number, string, and boolean (sometimes undefined and null). However, I feel it is also useful to know the rest as well once I become more fluent in JavaScript.

One useful function of JavaScript is that it has dynamic typing. This means that I do not have to manually define the data type of the value stored in a variable. Instead, data types are determined automatically.

좋은 웹페이지 즐겨찾기