> For the complete documentation index, see [llms.txt](https://mistborn.gitbook.io/til-coding/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mistborn.gitbook.io/til-coding/javascript/data-types.md).

# Data types

## A number

數字可以是整數或有小數點的數字，有許多運算符號可以使用 + - \* / ，有一些特殊的值 +-infinity NaN。

```javascript
alert( 1 / 0 ); // Infinity
alert( Infinity ); // Infinity
alert( "not a number" / 2 ); // NaN, such division is erroneous
alert( "not a number" / 2 + 5 ); // NaN
```

## A string

string 有 3 種表達方式。

```javascript
let str = "Hello";
let str2 = 'Single quotes are ok too';
let phrase = `can embed ${str}`;
```

反引號較特殊，把變數或運算式放進 ${...} 會自動運算結果。

```javascript
let name = "John";

// embed a variable
alert( `Hello, ${name}!` ); // Hello, John!

// embed an expression
alert( `the result is ${1 + 2}` ); // the result is 3
```

## A boolean (logical type)

布林值只有 2 種 `true` 或 `false` 。

## The “null” value

`null` 代表不存在、空的、沒有值。

## The “undefined” value

`undefined` 代表沒有指定值。

```javascript
let x;

alert(x); // shows "undefined"
```

## Objects and Symbols

以上的值都被稱為 primitive 因為都只能存一個值，物件比較特別可以存很多的資料，symbol 創造特別標誌的物件，也是 primitive。

## The typeof operator

`typeof` 返回變數的類型，有 2 種用法。

1. 運算式 `typeof x`
2. 函式 `typeof(x)`

```javascript
typeof undefined // "undefined"

typeof 0 // "number"

typeof true // "boolean"

typeof "foo" // "string"

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

typeof Math // "object" build-in object provides mathematical operations

typeof null // "object" 官方認證的錯誤

typeof alert // "function"  function is object but typeof treat them differently
```
