TIL coding
  • Initial page
  • 排版
  • Flexbox
  • Grid
  • jQuery
  • Untitled
  • JavaScript
    • An Introduction to JavaScript
    • Hello, world!
    • Code structure
    • The modern mode, "use strict"
    • Variables
    • Data types
    • Type Conversions
    • Operators
    • Comparisons
    • Interaction: alert, prompt, confirm
    • Conditional operators: if, '?'
    • Logical operators
    • Loops: while and for
    • The "switch" statement
    • Functions
    • Function expressions and arrows
    • JavaScript specials
    • Comments
    • Ninja code
    • Automated testing with mocha
    • Polyfills
    • Objects
    • Garbage collection
    • Symbol type
    • Object methods, "this"
    • Object to primitive conversion
    • Constructor, operator "new"
    • Methods of primitives
    • Numbers
    • Strings
    • Arrays
    • Array methods
    • Iterables
    • Map, Set, WeakMap and WeakSet
    • Object.keys, values, entries
    • Destructuring assignment
    • Date and time
    • JSON methods, toJSON
    • Recursion and stack
    • Rest parameters and spread operator
    • Closure
    • The old "var"
    • Global object
    • Function object, NFE
    • The "new Function" syntax
    • Scheduling: setTimeout and setInterval
    • Decorators and forwarding, call/apply
    • Function binding
    • Currying and partials
    • Arrow functions revisited
    • Property flags and descriptors
    • Property getters and setters
    • Prototypal inheritance
    • F.prototype
    • Native prototypes
    • Prototype methods, objects without __proto__
    • The “class” syntax
    • Class inheritance
    • Static properties and methods
    • Private and protected properties and methods
    • Extending built-in classes
    • Class checking: "instanceof"
    • Mixins
    • Error handling, "try..catch"
    • Custom errors, extending Error
    • Introduction: callbacks
    • Promise
    • Promises chaining
    • Error handling with promises
    • Promise API
  • Bootstrap
    • Navbar
Powered by GitBook
On this page
  • A number
  • A string
  • A boolean (logical type)
  • The “null” value
  • The “undefined” value
  • Objects and Symbols
  • The typeof operator

Was this helpful?

  1. JavaScript

Data types

A number

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

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 種表達方式。

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

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

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 代表沒有指定值。

let x;

alert(x); // shows "undefined"

Objects and Symbols

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

The typeof operator

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

  1. 運算式 typeof x

  2. 函式 typeof(x)

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

PreviousVariablesNextType Conversions

Last updated 5 years ago

Was this helpful?