search
HomeWeb Front-endJS TutorialWhat are typeof and instanceof in js? What's the difference?
What are typeof and instanceof in js? What's the difference?Oct 13, 2018 pm 04:35 PM
instanceoftypeofthe difference

This article mainly introduces to you the difference between typeof and instanceof in js.

First of all, everyone should briefly understand what typeof is?

typeof is a unary operation, placed before an operand, and the operand can be of any type. Its return value is a string describing the type of the operand. typeof can be used to detect the data type of a given variable.

What is instanceof?

The instanceof operator is used to determine whether the object pointed to by the prototype attribute of a constructor exists on the prototype chain of another object to be detected. Generally speaking, using instanceof is to determine whether an instance belongs to a certain type.

The similarities between typeof and instanceof in js:

Typeof and instanceof in JavaScript are often used to determine whether a variable is empty or what type it is.

Differences:

1. The definition and usage of typeof:

The return value is a string , used to describe the data type of the variable.

Specific usage details:

1. typeof generally can only return the following results:

'undefined': This value is undefined.

'boolean': This value is a Boolean value.

'string' : This value is a string.

'number' : This value is a numeric value.

'object': This value is an object or null.

'function' : This value is a function.
2. Use typeof to get whether a variable exists, such as

if(typeof a!="undefined"){alert("ok")}

instead of using if(a) because if a does not exist (not declared), an error will occur.

3. For special objects such as Array and Null, typeof always returns object. This is the limitation of typeof.
2. Instanceof definition and usage:

Instanceof definition and usage: instanceof is used to determine whether a variable belongs to an instance of an object. It can also be used to determine whether the prototype attribute of a certain constructor exists on the prototype chain of another object to be detected.

Example:

a instanceof b?alert("true"):alert("false"); //a是b的实例?真:假
 var a=new Array();alert(a instanceof Array);

will return true, and

alert(a instanceof Object)

will also return true;

This is because Array is an object subcategory.

Another example:

function test(){};var a=new test();alert(a instanceof test)

will return object.

Test:

var a=new Array();if (a instanceof Object) alert('Y');else alert('N');

got 'Y', and

if (window instanceof Object) alert('Y');else alert('N');

got 'N'.

So, the object tested by instanceof here refers to the object in js syntax, not the dom model object.

There will be some differences when using typeof:

alert(typeof(window))

You will get object.

This article is about the difference between typeof and instanceof in js. I hope it will be helpful to friends in need!

If you want to know more front-end knowledge points, you can follow the PHP Chinese website JavaScript Video Tutorial, Bootstrap Video Tutorial and other related tutorials. Welcome everyone to refer to and learn!

The above is the detailed content of What are typeof and instanceof in js? What's the difference?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
华为GT3 Pro和GT4的差异是什么?华为GT3 Pro和GT4的差异是什么?Dec 29, 2023 pm 02:27 PM

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

在C语言中,while(1)和while(0)之间的区别是什么?在C语言中,while(1)和while(0)之间的区别是什么?Aug 31, 2023 am 10:45 AM

我们知道在C语言中,'while'关键字用于定义一个循环,该循环根据传递给循环的条件来工作。现在,由于条件可以有两个值,即真或假,所以如果条件为真,则while块内的代码将被重复执行,如果条件为假,则代码将不会被执行。现在,通过将参数传递给while循环,我们可以区分while(1)和while(0),因为while(1)是一个条件始终被视为真的循环,因此块内的代码将开始重复执行。此外,我们可以说明,传递给循环并使条件为真的不是1,而是如果任何非零整数传递给while循环,则它将被视为真条件,因

win32和win64有什么区别win32和win64有什么区别May 29, 2023 pm 05:22 PM

win32和win64的区别是:1、win32是指Microsoft Windows操作系统的32位环境,win64是指Microsoft Windows操作系统的64位版本,比32位版本更加稳定快速;2、win32最高支持2G的内存,win64必须是4G以上内存;3、win64支持基于64位的处理器,而win32却不能完全支持;4、win32追求简洁,win64追求性能。

在C语言中,结构体(Structure)和数组(Array)之间的区别是什么?在C语言中,结构体(Structure)和数组(Array)之间的区别是什么?Aug 30, 2023 pm 09:37 PM

在C中,结构体和数组都用作数据类型的容器,即在结构体和数组中我们都可以存储数据,也可以对它们执行不同的操作。基于内部实现,以下是两者之间存在一些基本差异。Sr.编号键结构数组1定义结构体可以定义为一种数据结构,用作容器,可以容纳不同类型的变量。另一方面,数组是一种用作容器的数据结构,可以容纳相同类型的变量,但不支持多种数据类型变量。2内存分配输入数据的内存分配结构不必位于连续的内存位置。而在数组的情况下,输入数据存储在连续的内存分配中,这意味着数组将数据存储在分配连续内存块的内存模型中(即,具有

JavaScript和PHP的cookie之间有哪些区别?JavaScript和PHP的cookie之间有哪些区别?Sep 02, 2023 pm 12:29 PM

JavaScriptCookie使用JavaScriptcookie是记住和跟踪偏好、购买、佣金和其他信息的最有效方法。更好的访问者体验或网站统计所需的信息。PHPCookieCookie是存储在客户端计算机上的文本文件并保留它们用于跟踪目的。PHP透明地支持HTTPcookie。JavaScriptcookie如何工作?您的服务器将一些数据发送到访问者的浏览器cookie的形式。浏览器可以接受cookie。如果存在,它将作为纯文本记录存储在访问者的硬盘上。现在,当访问者到达站点上的另一个页面时

Vue3和Vue2的区别:更丰富的生命周期钩子Vue3和Vue2的区别:更丰富的生命周期钩子Jul 08, 2023 pm 05:19 PM

Vue3和Vue2的区别:更丰富的生命周期钩子Vue是一种流行的JavaScript框架,用于构建交互式的Web应用程序。Vue2是Vue.js的稳定版本,而Vue3是Vue.js的最新版本。Vue3带来了许多改进,其中之一是更丰富的生命周期钩子。本文将介绍Vue3和Vue2生命周期钩子的区别,并通过代码示例进行演示。Vue2的生命周期钩子在Vue2中,我们

win7企业版与win7旗舰版有什么区别的详细介绍win7企业版与win7旗舰版有什么区别的详细介绍Jul 14, 2023 pm 08:37 PM

win7有好几个版本,对于这些琳琅满目的win7版本,很多朋友也是不知道这些win7版本有什么区别了,功能上哪个更好,哪个系统更适合自己,这里小编和大家介绍下win7企业版与win7旗舰版有什么区别的详细介绍,大家快来看看吧。1、Windows7企业版(Enterprise)这个版本面向企业市场的高级版本,主要对象是企业用户以及其市场,满足企业数据共享、管理、安全等需求。包含多语言包、UNIX应用支持、BitLocker驱动器加密、分支缓存(BranchCache)等。通过大量授权给有与微软签订

深入比较Tomcat和Nginx的不同之处深入比较Tomcat和Nginx的不同之处Dec 28, 2023 am 09:47 AM

Tomcat与Nginx是两个常用的互联网服务器软件,它们在功能和设计理念上有很多区别。本文将深入探讨Tomcat与Nginx的区别,以帮助读者更好地了解这两个软件。首先,Tomcat是一个开源的JavaServlet容器,主要用于支持Java应用程序的运行。它是一个完整的应用服务器,内置了Servlet容器和JSP支持,并提供了一系列的Java类库用于开

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.