zhaoJian's Tech Notes

IT Course JavaScript Basics 038_Data Types

Learning / JavaScript Basics ~5683 words · 15 min read - views

In JavaScript, data types are used to represent different kinds of data, such as numbers, strings, boolean values, etc. Data types in JavaScript are divided into two categories: primitive data types and object data types.

Primitive Data Types

Primitive data type values contain only a single piece of content (string, number, or others).

Number

Used to represent numbers, including integers and floating-point numbers. For example: 5, 3.14, -10. Numbers can have many operations, such as multiplication *, division /, addition +, subtraction -, etc. Besides regular numbers, there are also “special numeric values” that belong to this type: Infinity, -Infinity, and NaN. The Number type is a floating-point type and may have precision issues when performing exact calculations, such as: 0.1 + 0.2. You can convert a string to an integer using the parseInt() method, and convert a string to a floating-point number using the parseFloat() method.

Example:

let age = 25;
console.log(0.1 + 0.2); // Output 0.30000000000000004
let result = 0.1 * 10 + 0.2 * 10;
result = result / 10; // Convert the result back to a floating-point number
console.log(result); // Output 0.3
BigInt

A data type representing arbitrary precision integers (with respect to memory size limitations). It allows you to represent integer values in a larger range than the Number type without losing precision. Use the n suffix or call the BigInt() constructor to create a BigInt. It handles integer values that exceed the Number range, cannot be mixed with regular integers, and cannot be converted via parseInt() or parseFloat().

Example:

let bigIntLiteral = 123n;
let bigIntObject = BigInt(456);
String

Used to represent text data. A string consists of a series of characters and can contain letters, numbers, symbols, etc. For example: “Hello, World!”, “你好”, ‘123’. Strings must be enclosed in quotes, including: single quotes 'Hello', double quotes "Hello", backticks `Hello`. Single and double quotes are “simple” quotes. Backticks are quotes with extended functionality that can directly reference variables using ${…} and preserve text formatting. Newline \n, tab \t, backslash itself \\, single quote \' and double quote \", carriage return \r

Example:

let text = "Hello, World!";
Boolean

Used to represent logical values, with only two possible values: true or false.

Example:

let isStudent = true;
Undefined

Used to represent uninitialized, undefined, unassigned variables or functions without parameter values.

Example:

let undefinedVar;
alert(undefinedVar);
Null

Used to represent “nothing”, “empty”, or “value unknown” special values or non-existent object values.

Example:

let nullValue = null;
Symbol

Used to create unique, immutable identifiers. Commonly used to define private properties of objects, create unique constants, or as identifiers for special behavior. Every symbol created through Symbol is unique, even if they have the same description.

Example:

const symbol1 = Symbol();
const symbol2 = Symbol();
console.log(symbol1 === symbol2); // Output false, each Symbol is unique

Object Data Types

Can be used to store collections of data or more complex content.

Object

Used to store key-value pairs, or the mapping relationship between properties and property values. Objects can contain values of various data types, including numbers, strings, boolean values, arrays, other objects, etc.

Example:

let person = {
name: "Alice",
age: 30,
isStudent: false
};
Array

An array is an ordered, mutable data structure used to store a group of values. Arrays can contain elements of different data types, and each element has a corresponding index.

Example:

let numbers = [1, 2, 3, 4, 5];
Function

A function is a reusable block of code that can accept input parameters, perform a series of operations, and return results. Functions are first-class citizens in JavaScript. They can be assigned to variables, passed as arguments to other functions, and can also be used as return values of other functions.

Example:

function greet(name) {
console.log("Hello, " + name + "!");
}
greet("JavaScript");

typeof

typeof is an operator in JavaScript used to get the type of a given variable (so parentheses are not needed in typeof). It returns a string representing the variable type.

Example:

let undefinedVariable;
let booleanVariable = true;
let numberVariable = 42;
let stringVariable = "Hello";
let objectVariable = { key: "value" };
let functionVariable = function() {};
let symbolVariable = Symbol("mySymbol");
let nullVariable = null;
console.log(typeof undefinedVariable); // Output "undefined"
console.log(typeof booleanVariable); // Output "boolean"
console.log(typeof numberVariable); // Output "number"
console.log(typeof stringVariable); // Output "string"
console.log(typeof objectVariable); // Output "object"
console.log(typeof functionVariable); // Output "function"
console.log(typeof symbolVariable); // Output "symbol"
console.log(typeof nullVariable); // Output "object"

Using typeof x, x will return the variable type and variable value respectively. First execute typeof x, then execute x, and output two values.

Example:

let x = 42;
console.log(typeof x, x);
  • typeof x returns “number”, indicating that the type of variable x is number.
  • x returns the value of variable x, which is 42.
Share:

Comments