Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ชนิดข้อมูลแบบซับซ้อน (Complex Data Types) ใน JavaScript

ชนิดข้อมูลแบบซับซ้อนใน JavaScript รวมถึงตัวดำเนินการ typeof ตัวดำเนินการ typeof เป็น unary operator ที่วางก่อนตัวถูกดำเนินการตัวเดียว ซึ่งสามารถเป็นชนิดใดก็ได้ ค่าของมันเป็นสตริงที่ระบุชนิดข้อมูลของตัวถูกดำเนินการ

ตัวดำเนินการ typeof ประเมินค่าเป็น "number", "string", หรือ "boolean" หากตัวถูกดำเนินการเป็นตัวเลข สตริง หรือค่าบูลีน และคืนค่า true หรือ false ตามการประเมิน

ตัวอย่าง

ลองรันโค้ดด้านล่างเพื่อเรียนรู้การทำงานกับตัวดำเนินการ typeof ใน JavaScript

<html>
  <body>
    <script>
      <!--
        var a = 10;
        var b = "String";
        var linebreak = "<br />";
        result = (typeof b == "string" ? "B is String" : "B is Numeric");
        document.write("Result => ");
        document.write(result);
        document.write(linebreak);
        result = (typeof a == "string" ? "A is String" : "A is Numeric");
        document.write("Result => ");
        document.write(result);
        document.write(linebreak);
      //-->
    </script>
  </body>
</html>