首页/javascript外包/ typeof类型 js中typeof的用法

typeof类型 js中typeof的用法

发布-xiaoming | 浏览量-

 JS中的变量是松散类型(即弱类型)的,可以用来保存任何类型的数据。

1,typeof操作符。
typeof 可以用来检测给定变量的数据类型,可能的返回值:
typeof操作符是用来检测变量的数据类型。使用:typeof  变量名;返回以下字符串:
字符串 描述
undefined 未定义
boolean 布尔值
string 字符串
number 数值
object 对象或者null
function 函数

2,undefined类型

undefined只是一个值。当我们声明一个变量,没有做初化的时候。我们调用这个变量就会返回一个值undefined。如:

var name; alert(name);如果我们把alert语句写成alert(people);同样是返回undefined。这两种情况分别是一、只是定义了变量没有做相应的

初始化。二、没有声明相应的变量。调用报错。ps:在声明变量的时候最好进行初始化。

3,null类型。

null是一个只有一个值的特殊类型。表示一个空对象引用。用typeof检测返回是object。

undefined是派生自null。undefined==null。

4,Boolead类型。

Boolean 类型有两个值(字面量): true 和 false。 而 true 不一定等于 1, false 不一定等于 0。 JavaScript 是区分大小写的,True 和 False 或者其他都不是 Boolean 类型的值。boolean可以与其他类型转化。

以下是其他类型转换成 Boolean 类型规则:

数据类型  转换为 true 的值 转换为 false 的值
Boolean true false
String  任何非空字符串  空字符串
Number  任何非零数字值(包括无穷大) 0 和 NaN

 Object

任何对象 null
Undefined  

undefined

 

5.Number 类型

Number 类型包含两种数值:整型和浮点型。

通过 Number.POSITIVE_INFINITY 和 Number.NEGATIVE_INFINITY 得到 Infinity(正无穷)及-Infinity(负无穷)的值。 alert(Number.POSITIVE_INFINITY); //Infinity(正无穷) alert(Number.NEGATIVE_INFINITY);//-Infinity(负无穷)

var box = 12 / 0; //Infinity

6.String 类型

String 类型用于表示由于零或多个 16 位 Unicode 字符组成的字符序列,即字符串。字 符串可以由双引号(")或单引号(')表示。

Js代码 
           
var aa = 'test string';   
alert(typeof aa);  // 'string'   
alert(typeof 90);  // 'number'  
 
 
写了代码测试了下
JavaScript Code复制内容到剪贴板
  1. <script>  
  2. document.write ("typeof(1): "+typeof(1)+"<br>");  
  3. document.write ("typeof(NaN): "+typeof(NaN)+"<br>");  
  4. document.write ("typeof(Number.MIN_VALUE): "+typeof(Number.MIN_VALUE)+"<br>")  
  5. document.write ("typeof(Infinity): "+typeof(Infinity)+"<br>")  
  6. document.write ("typeof(\"123\"): "+typeof("123")+"<br>")  
  7. document.write ("typeof(true): "+typeof(true)+"<br>")  
  8. document.write ("typeof(window): "+typeof(window)+"<br>")  
  9. document.write ("typeof(document): "+typeof(document)+"<br>")  
  10. document.write ("typeof(null): "+typeof(null)+"<br>")  
  11. document.write ("typeof(eval): "+typeof(eval)+"<br>")  
  12. document.write ("typeof(Date): "+typeof(Date)+"<br>")  
  13. document.write ("typeof(sss): "+typeof(sss)+"<br>")  
  14. document.write ("typeof(undefined): "+typeof(undefined)+"<br>")  
  15. </script>  
 
结果如下
typeof(1): number
typeof(NaN): number
typeof(Number.MIN_VALUE): number
typeof(Infinity): number
typeof("123"): string
typeof(true): boolean
typeof(window): object
typeof(document): object
typeof(null): object
typeof(eval): function
typeof(Date): function
typeof(sss): undefined
typeof(undefined): undefined
 

Math.floor是什么?JavaScript floor() 方法

Javascript 中的false、0、null、undefined和空字符串对象

js中return;、return true、return false;区别

jquery number() parseInt() parseFloat()

js字符串函数|JS截取字符串函数

 

原文地址:http://www.35ui.cn/post/20160204391.html

标签jsjavascriptjs手册typeof

上一条: url传中文参数乱码 url参数乱码
下一条: 黄色网站 最黄色网站网址在这里

或许你还对下面的文章感兴趣