Written at the beginning:
During yesterday’s interview, I found that I didn’t answer a very, very simple question. Maybe it was because I was too nervous, and I felt like I was making myself cry. Later, I thought about it and should record it carefully so that I can have a deep impression. The revolution has not yet succeeded, and we still need to work hard to become strong!
1. The six major data types of js
number: numbers, integers, floating point numbers, etc.,
string: single quotes or double quotes to indicate,
Boolean: Returns true and false, these two values do not necessarily correspond to 1 and 0
object: object, which can be created by executing the new operator followed by the name of the object type to be created.
null: There is only one value data type. Logically speaking, the null value represents a null object pointer.
undefined: Undefined. When a variable is declared using var but not initialized, the value of the variable is undefined.
2. Data type judgment typeof
typeof can solve the judgment of most data types. Its return value is a string, which describes the type of the operand.
//判断变量num是不是一个数字类型 if(typeof num=='number') { return true; }
Return results:
var a="hling"; console.log(a); //string var a=1; console.log(a); //number var a=false; console.log(a); //boolean var a; console.log(typeof a); //undfined var a = null; console.log(typeof a); //object var a = document; console.log(typeof a); //object var a = []; console.log(a); //object var a = function(){}; console.log(typeof a) //function除了可以判断数据类型还可以判断function类型
In addition to the four types of string, number, boolean, and undefined, null, object, and array return They are all of object type! ! !
For function types, function is returned, such as typeof(Date), typeof(eval), etc.
3. How to determine array type in js
1) instanceof
instanceof is a ternary operator used to determine whether a variable is an instance of an object. This operator has something to do with the object orientation in JavaScript. To understand this, you must first understand the object orientation in JavaScript. Because this operator detects whether the object's prototype chain points to the prototype object of the constructor.
a instanceof b?alert("true"):alert("false") //注意b值是你想要判断的那种数据类型,是不是一个字符串,比如Array
Example:
var arr = [1,2,3,1]; alert(arr instanceof Array); // true
2) constructor
is defined in the W3C definition: The constructor attribute returns the key to creating this References to array functions of objects
var arr = []; arr instanceof Array; // true arr.constructor == Array; //true
The method for judging various types is:
console.log("string".constructor == String); console.log((123).constructor == Number); console.log(false.constructor == Boolean); console.log([].constructor == Array); console.log({}.constructor == Object);
General method:
function isArray(object){ return object && typeof object==='object' && Array == object.constructor; }
3) Characteristic judgment
object.isArray() to judge, the purpose is to accurately detect whether a value is an array. IE9+, Firefox 4+, Safari 5+, Opera 10.5+ and Chrome all implement this method. However, versions before IE8 are not supported.
function isArray(object){ return object && typeof object==='object' && typeof object.length==='number' && typeof object.splice==='function' && //判断length属性是否是可枚举的 对于数组 将得到false !(object.propertyIsEnumerable('length')); }
4) Object.prototype.toString.call
Object.prototype.toString.call(value) == '[object Array]'
The above article briefly talks about js data type judgment and array judgment. This is the editor I have shared all the content with you, I hope it can give you a reference, and I also hope you will support the PHP Chinese website.
For more articles on js data type judgment and array judgment, please pay attention to the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool