Javascript 中有 5 种始数据类型

Undefined

Null: 值只有一个 null

Boolean: 值有两个 true false (var a = false;)

String

Number

1. Undefined: 值只有一个 undefined

<html>
  <head>
    <script type="text/javascript">
        var s;
        alert(s);
    </script>
  </head>
  
  <body>
    This is my HTML page. <br>
  </body>
</html>

结果: undefined (小写,表示数据类型 Undefined的实际值)

4. String: 定义时可用单引号,也可用双引号。(注: js中没有 char 类型)

判断一个变量的类型可以用 typeof

typeof 是 一元运算符。后跟变量名称,用于获取数据类型。返回值有5个:undefined, string, number, boolean 和 object.

<html>
  <head>
    <script type="text/javascript">
        var s = "hello"; //s是原始类型
        alert(typeof s);
    </script>
  </head>
  <body>
  </body>
</html>

结果: string

如果我们改成

<html>
  <head>
    <script type="text/javascript">
        var s = new String("hello"); //s是原始类型
        alert(typeof s);
    </script>
  </head>
  <body>
  </body>
</html>

结果: Object

Object是所有类型的总称。

 

相关文章:

  • 2022-01-04
  • 2022-02-13
  • 2022-12-23
  • 2022-12-23
  • 2022-03-03
  • 2022-01-02
  • 2021-12-23
猜你喜欢
  • 2022-02-08
  • 2022-12-23
  • 2021-10-31
  • 2021-10-22
  • 2022-02-01
  • 2022-12-23
  • 2021-11-23
相关资源
相似解决方案