JS检测数据类型详解

一、js检测数据类型方法

JS中内置了用于检测数据类型的方法,其中最常用的是typeof和instanceof。

// 使用typeof检测数据类型
console.log(typeof 123); // 输出 "number"
console.log(typeof "hello"); // 输出 "string"
console.log(typeof true); // 输出 "boolean"
console.log(typeof undefined); // 输出 "undefined"
console.log(typeof null); // 输出 "object"
console.log(typeof []); // 输出 "object"
console.log(typeof {}); // 输出 "object"
console.log(typeof function(){}); // 输出 "function"

// 使用instanceof检测数据类型
console.log(123 instanceof Number); // 输出 false
console.log("hello" instanceof String); // 输出 false
console.log(true instanceof Boolean); // 输出 false
console.log(undefined instanceof undefined); // 输出报错
console.log(null instanceof null); // 输出 false
console.log([] instanceof Array); // 输出 true
console.log({} instanceof Object); // 输出 true
console.log((function(){}) instanceof Function); // 输出 true

二、js如何检测数据类型

在JS中,除了使用内置的检测数据类型方法外,还可以使用typeof+值的形式来进行数据类型的判断。

// 使用typeof+值来检测数据类型
console.log(typeof 123 === "number"); // 输出 true
console.log(typeof "hello" === "string"); // 输出 true
console.log(typeof true === "boolean"); // 输出 true
console.log(typeof undefined === "undefined"); // 输出 true
console.log(typeof null === "object"); // 输出 true
console.log(typeof [] === "object"); // 输出 true
console.log(typeof {} === "object"); // 输出 true
console.log(typeof function(){} === "function"); // 输出 true

三、检测数据类型js

在JS中,有多种方法可以检测数据类型,如使用typeof、instanceof、Object.prototype.toString等。

// 使用Object.prototype.toString来检测数据类型
console.log(Object.prototype.toString.call(123) === "[object Number]"); // 输出 true
console.log(Object.prototype.toString.call("hello") === "[object String]"); // 输出 true
console.log(Object.prototype.toString.call(true) === "[object Boolean]"); // 输出 true
console.log(Object.prototype.toString.call(undefined) === "[object Undefined]"); // 输出 true
console.log(Object.prototype.toString.call(null) === "[object Null]"); // 输出 true
console.log(Object.prototype.toString.call([]) === "[object Array]"); // 输出 true
console.log(Object.prototype.toString.call({}) === "[object Object]"); // 输出 true
console.log(Object.prototype.toString.call(function(){}) === "[object Function]"); // 输出 true

四、js检测数据类型是不是数字

在JS中,可以使用typeof或isNaN方法来检测一个值是否为数字类型。

var num = 123;
console.log(typeof num === "number"); // 输出 true
console.log(!isNaN(num)); // 输出 true

var str = "123";
console.log(typeof str === "number"); // 输出 false
console.log(!isNaN(str)); // 输出 true

var bool = true;
console.log(typeof bool === "number"); // 输出 false
console.log(!isNaN(bool)); // 输出 true

var undef = undefined;
console.log(typeof undef === "number"); // 输出 false
console.log(isNaN(undef)); // 输出 true

var nul = null;
console.log(typeof nul === "number"); // 输出 false
console.log(!isNaN(nul)); // 输出 true

五、js检测数据类型的操作符

在JS中,有三种操作符可以用于检测变量的数据类型,分别为typeof、instanceof、和Object.prototype.toString.call()。

var num = 123;
console.log(typeof num); // 输出 "number"
console.log(num instanceof Number); // 输出 false
console.log(Object.prototype.toString.call(num)); // 输出 "[object Number]"

var str = "hello";
console.log(typeof str); // 输出 "string"
console.log(str instanceof String); // 输出 false
console.log(Object.prototype.toString.call(str)); // 输出 "[object String]"

var bool = true;
console.log(typeof bool); // 输出 "boolean"
console.log(bool instanceof Boolean); // 输出 false
console.log(Object.prototype.toString.call(bool)); // 输出 "[object Boolean]"

var arr = [];
console.log(typeof arr); // 输出 "object"
console.log(arr instanceof Array); // 输出 true
console.log(Object.prototype.toString.call(arr)); // 输出 "[object Array]"

var obj = {};
console.log(typeof obj); // 输出 "object"
console.log(obj instanceof Object); // 输出 true
console.log(Object.prototype.toString.call(obj)); // 输出 "[object Object]"

var fn = function(){};
console.log(typeof fn); // 输出 "function"
console.log(fn instanceof Function); // 输出 true
console.log(Object.prototype.toString.call(fn)); // 输出 "[object Function]"

六、js检测数据类型的方法有哪些

JS中检测数据类型的方法有很多种,如上文所述,最常用的是typeof、instanceof、和Object.prototype.toString.call()。

除此之外,还有以下方法:

  • 1.使用constructor方法进行数据类型的判断
  •     var num = new Number(123);
        console.log(num.constructor === Number); // 输出 true
        
        var str = new String("hello");
        console.log(str.constructor === String); // 输出 true
        
        var bool = new Boolean(true);
        console.log(bool.constructor === Boolean); // 输出 true
        
        var arr = [];
        console.log(arr.constructor === Array); // 输出 true
        
        var obj = {};
        console.log(obj.constructor === Object); // 输出 true
        
        var fn = function(){};
        console.log(fn.constructor === Function); // 输出 true
        
  • 2.使用isPrototypeOf方法进行数据类型的判断
  •     var num = new Number(123);
        console.log(Number.prototype.isPrototypeOf(num)); // 输出 true
        
        var str = new String("hello");
        console.log(String.prototype.isPrototypeOf(str)); // 输出 true
        
        var bool = new Boolean(true);
        console.log(Boolean.prototype.isPrototypeOf(bool)); // 输出 true
        
        var arr = [];
        console.log(Array.prototype.isPrototypeOf(arr)); // 输出 true
        
        var obj = {};
        console.log(Object.prototype.isPrototypeOf(obj)); // 输出 true
        
        var fn = function(){};
        console.log(Function.prototype.isPrototypeOf(fn)); // 输出 true
        
  • 3.使用Array.isArray方法判断是否为数组类型
  •     var arr = [];
        console.log(Array.isArray(arr)); // 输出 true
        
        var obj = {};
        console.log(Array.isArray(obj)); // 输出 false
        

七、检测数据类型的四种方式

JS中检测数据类型的四种方式是:typeof、instanceof、Object.prototype.toString.call()和constructor。

var num = 123;
console.log(typeof num === "number"); // 输出 true
console.log(num instanceof Number); // 输出 false
console.log(Object.prototype.toString.call(num)); // 输出 "[object Number]"
console.log(num.constructor === Number); // 输出 true

var str = "hello";
console.log(typeof str === "string"); // 输出 true
console.log(str instanceof String); // 输出 false
console.log(Object.prototype.toString.call(str)); // 输出 "[object String]"
console.log(str.constructor === String); // 输出 true

var bool = true;
console.log(typeof bool === "boolean"); // 输出 true
console.log(bool instanceof Boolean); // 输出 false
console.log(Object.prototype.toString.call(bool)); // 输出 "[object Boolean]"
console.log(bool.constructor === Boolean); // 输出 true

var arr = [];
console.log(typeof arr === "object"); // 输出 true
console.log(arr instanceof Array); // 输出 true
console.log(Object.prototype.toString.call(arr)); // 输出 "[object Array]"
console.log(arr.constructor === Array); // 输出 true

var obj = {};
console.log(typeof obj === "object"); // 输出 true
console.log(obj instanceof Object); // 输出 true
console.log(Object.prototype.toString.call(obj)); // 输出 "[object Object]"
console.log(obj.constructor === Object); // 输出 true

var fn = function(){};
console.log(typeof fn === "function"); // 输出 true
console.log(fn instanceof Function); // 输出 true
console.log(Object.prototype.toString.call(fn)); // 输出 "[object Function]"
console.log(fn.constructor === Function); // 输出 true

八、怎样检测数据类型

在JS中,我们可以使用多种方式来检测一个值的数据类型,如 typeof、instanceof、Object.prototype.toString.call()等。

对于基本数据类型,可以使用typeof或者isNaN方法来检测其数据类型;对于引用类型如数组和对象,可以使用instanceof、Object.prototype.toString.call()或者constructor方法来进行判断。

原创文章,作者:PTANB,如若转载,请注明出处:https://www.506064.com/n/371706.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
PTANBPTANB
上一篇 2025-04-23 18:08
下一篇 2025-04-23 18:08

相关推荐

  • JS Proxy(array)用法介绍

    JS Proxy(array)可以说是ES6中非常重要的一个特性,它可以代理一个数组,监听数据变化并进行拦截、处理。在实际开发中,使用Proxy(array)可以方便地实现数据的监…

    编程 2025-04-29
  • Python返回数组:一次性搞定多种数据类型

    Python是一种多用途的高级编程语言,具有高效性和易读性的特点,因此被广泛应用于数据科学、机器学习、Web开发、游戏开发等各个领域。其中,Python返回数组也是一项非常强大的功…

    编程 2025-04-29
  • Python 转换数据类型

    本文将详细探讨Python中转换数据类型的方法和技巧,帮助大家更好地处理不同类型的数据。 一、数据类型概述 在Python中,常用的数据类型包括字符串、整数、浮点数、列表、元组、字…

    编程 2025-04-29
  • Python数据类型分为哪几种

    Python作为一门非常灵活的编程语言,有着非常丰富的数据类型。Python的数据类型可以分为数字类型、字符串类型、列表类型、元组类型、字典类型和集合类型六种。 一、数字类型 Py…

    编程 2025-04-29
  • 解析js base64并转成unit

    本文将从多个方面详细介绍js中如何解析base64编码并转成unit格式。 一、base64编码解析 在JavaScript中解析base64编码可以使用atob()函数,它会将b…

    编程 2025-04-29
  • Node.js使用Body-Parser处理HTTP POST请求时,特殊字符无法返回的解决方法

    本文将解决Node.js使用Body-Parser处理HTTP POST请求时,特殊字符无法返回的问题。同时,给出一些相关示例代码,以帮助读者更好的理解并处理这个问题。 一、问题解…

    编程 2025-04-29
  • t3.js:一个全能的JavaScript动态文本替换工具

    t3.js是一个非常流行的JavaScript动态文本替换工具,它是一个轻量级库,能够很容易地实现文本内容的递增、递减、替换、切换以及其他各种操作。在本文中,我们将从多个方面探讨t…

    编程 2025-04-28
  • JS图片沿着SVG路径移动实现方法

    本文将为大家详细介绍如何使用JS实现图片沿着SVG路径移动的效果,包括路径制作、路径效果、以及实现代码等内容。 一、路径制作 路径的制作,我们需要使用到SVG,SVG是可缩放矢量图…

    编程 2025-04-27
  • 如何使用JS调用Python脚本

    本文将详细介绍通过JS调用Python脚本的方法,包括使用Node.js、Python shell、child_process等三种方法,以及在Web应用中的应用。 一、使用Nod…

    编程 2025-04-27
  • Python数据类型操作题

    本文将从多个方面对Python数据类型操作题进行详细阐述,并给出相应的代码示例。 一、列表 列表是Python中的常见数据类型之一,可以存储各种类型的对象。下面是一些常见的列表操作…

    编程 2025-04-27

发表回复

登录后才能评论