iT邦幫忙

2025 iThome 鐵人賽

DAY 11
0

在JavaScript中,資料型別主要分為基礎型別物件型別

基礎型別 (Primitive Data Types)

Number (數值)

  • 可以表示整數與浮點數。
    常見數值運算:+、-、*、/、%、**、++、--、+= 等等。

    let x = 10;
    x++;   // 11
    x--;   // 9
    x /= 2 // 5
    

    型別轉換:

    Number("20");     // 20
    parseInt("10");   // 10
    parseInt("1.23"); // 1
    parseFloat("1.23"); // 1.23
    +"20"; // 20 (一元 + 轉型數字)
    

    特殊數值:NaN (Not a Number)

    typeof NaN; //"number"
    Number("abc"); //NaN
    

    數字方法 (Methods):

    let pi = 3.14159;
    pi.toFixed(2);   //"3.14"
    pi.toString();   //"3.14159"
    
    • 注意:0.1 + 0.2 === 0.3 會是 false,因為二進制無法精確表示所有小數。

String (字串)

  • 用單引號 ' ' 或雙引號 " " 包起來。

  • 串接使用 + 或字串模板 (ES6):

    let name = "Spark";
    let intro = `Hello, my name is ${name}`;
    

    轉型成字串:

    String(123); //"123"
    2021 + "";   //"2021"
    

    常見方法:

    let str = "Wilson";
    str.length;         // 6
    str.slice(2,5);     // "lso"
    str.indexOf("son"); // 3
    str.toUpperCase();  // "WILSON"
    "Hello world".split(" "); // ["Hello","world"]
    

Boolean (布林)

  • 只有 truefalse

  • 常用於條件判斷:

    if (x > 10) { ... }
    

    型別轉換:

    Boolean("hello"); // true
    Boolean("");      // false
    Boolean(0);       // false
    Boolean(123);     // true
    

    真值 (Truthy) 與 假值 (Falsy)

    • Truthy:非空字串、非零數字、物件、陣列、函式
    • Falsy:0、NaN、""、false、null、undefined

Null (空值)

  • typeof null === "object" 是一個歷史 bug。

Undefined (未定義)

  • 變數被宣告但未賦值時,預設為 undefined。

    let x;
    console.log(x); // undefined
    

    和 not defined 不一樣:

    console.log(y); // ReferenceError: y is not defined
    

Symbol (ES6 新增)

  • 表示唯一的識別符,用於避免物件屬性衝突。

BigInt

  • 表示任意長度的整數。
    let big = 123456789012345678901234567890n;
    

物件型別 (Non-Primitive Data Types)

Object (物件)

  • key-value pair 組成,可用 {} 建立。
  • 存取方式:obj.keyobj["key"]
    let person = {
    name: "Peter",
    score: 100,
    father: { name: "Nick" }
    };
    console.log(person.name);        // "Peter"
    console.log(person.father.name); // "Nick"
    
  • typeof [] 回傳 "object",可以用 Array.isArray(arr) 確認是否為陣列。

Array (陣列)

  • 具順序的資料集合,可存放不同型別。
    let arr = [1, "hi", true];
    console.log(arr[0]); // 1
    

Function (函式)

  • 其實也是物件的一種,可以有屬性和方法。
    function sayHi(name) {
      console.log(`Hi ${name}`);
    }
    sayHi("Wilson");
    

typeof 運算子

  • 用來檢查資料型別,結果會回傳字串:
    typeof "hello"; // "string"
    typeof 100;     // "number"
    typeof true;    // "boolean"
    typeof {};      // "object"
    typeof [];      // "object"
    typeof function(){}; // "function"
    typeof undefined; // "undefined"
    typeof null; // "object" (bug)
    

運算子補充

  • 比較運算子 (Comparison Operators):==、===、!=、!==、>、<、>=、<=
  • 邏輯運算子 (Logical Operators):&&、||、!
  • 位元運算子 (Bitwise Operators):&、|、^、~、<<、>>
  • 運算子優先序:先乘除、後加減,括號優先。

參考資料


上一篇
Day10|Node Package Manager
下一篇
Day12|變數(Variable)
系列文
程式小白的 30 天轉職挑戰14
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言