JavaScript类型和语法

JavaScript类型和语法

一、类型

  1、内置类型(null、undefined、boolean、number、string、object、symbol(es6中新增))(除对象之外,其它统称为基本类型);

         可以用typeof去判断值的类型。

         typeof undefined    ===   "undefined"; //true

    typeof true             ===    "boolean"; //true

    typeof 20        ===   "number"; //true

    typeof "20"        ===   "string";// true

    typeof {life: 20}       ===   “object”;// true    object的一个子类型

    typeof [1,2,3]     ===   "object"; //true    object的一个子类型

    typeof Symbor()     ===   "symbol";  //true    es6中新增类型

    至于  typeof null     ===    "object";//true(它是一个bug,但是还没有修复;如果改过来,许多系统无法正常工作。)

    检测null值类型的方法:var a = null;      (!a = typeof a === "object"); //true;   null是object的一个子类型。

  2、值和类型(JavaScript中的变量是没有类型的,只有值才有);

    在对变量执行typeof操作时,判断的不是变量的类型,而是该变量持有的值的类型。类型定义了值的行为特征。

    例:   var a = 20;     tyepeof  a;  //"number"

       a = true;   typeof a;  //boolean;

       typeof运算符总是返回一个字符串:  typeof typeof 42;// "string"    typeof 20  首先返回字符 “number”  ;然后typeof "number" 返回 “string”

  3、undefined 和 undeclared

    在作用域中声明但还没有赋值的变量是undefined;还没有在作用域中声明的变量,是undeclared

    例; var a;    typeof a;  //underfined      typeof  c;//ReferenceError  c is not  defined         但是typeof类型都是undefined

     

未完待续。。。。。

 

  

原文地址:https://www.cnblogs.com/yaosusu/p/11408163.html