Detailed explanation of variables and data types in js
1. Variables
Variables in js are an abstract concept. Variables are used to store values and represent values. Defining a variable in js is very simple: var variable name = variable value
= is an assignment operation, with the variable name on the left and the stored value on the right
Variables in js are loosely typed : Any data type can be stored through a var variable name
For example, var name = '李思'
2. Data type
1. Basic data types: composed of simple structures
Number (number), string (string), Boolean (boolean), null, undefined
2. Reference data type: The structure is relatively complex
Object data type (object)
Function data type (function)
3. Detailed explanation of data types
1. Number: positive number, negative number, 0, decimal NaN (not a valid number, but belongs to the number data type )
## NaN==NaN //false is not equal
var num = 12 //= is assignment == is to determine whether the values on the left and right sides are Equality isNaN(); tests whether a proposition whose value is not a valid number is true. If it is a valid number, it returns false. If it is not a valid number, it returns true If the value detected is not of type number , the browser will convert it into number by default Number(): Forces other data types to be converted into number types. It is required that if it is a string, all the strings must be numbers before it can be converted For example: Number('12')==>12 Number('12px')==>NaN Non-mandatory data type conversion parseInt/parseFloat: parseInt: Search characters one by one from left to right, converting numbers into valid numbers. If you encounter a non-valid number in the middle, you will not continue searching ParseFloat: Same as the above one, you can Identify one more decimal point Interview questions:var val = Number('12px');if(val==12){ console.log(12) }else if(val==NaN){ console.log(NaN) }else{ console.log('以上都不成立')//输出这个 }
2. boolean: true false
! : An exclamation point is negated. First convert the value into a Boolean type, and then negate it
console.log(!3)//Convert 3 to boolean first, and then negate it ! ! : Convert other data types into boolean types, equivalent to Boolean() Rules for data type conversion: 1) If there is only one value, determine whether this value is true Or false, follow: only 0 NaN " " null undefined These five are false and the rest are true2) If two values are compared to see if they are equal
val1==val2 The two values may not be of the same data type. If compared with ==, the default data type conversion will be performed
①Object == object, never equal
②、Object == string, first convert the object into a string (call the toString method), and then compare it
[]Convert into a string ""
{}Convert to string "[object,object]"
③、Object == Boolean type, the object is first converted into a string (toString), and then converted into a number (Number "" becomes 0), the Boolean type is also converted to a number (true is 1, false is 0), the last two numbers are compared
④. Object == number, the object is first converted into a string (toString), and then converted is a number (Number "" becomes 0), the last two numbers are compared
⑤、Number == Boolean, Boolean is converted into a number,
⑥、 Number == string, string Capture it into a number
⑦、String == Boolean are all converted into numbers
ulous in in Boolean, all are converted intoâ€Ŧ The data types are not equal in comparison
3), except == is a comparison, === is also a comparison (absolute comparison)
val1===val2 If the data The types are not equal, definitely not equal
3. Object data type (composed of multiple groups [attribute name and attribute value], multiple groups of key-value pairs, multiple key:value. Attribute name and attribute The value is used to describe the characteristics of this object)For example: literal creation method instance creation method
personInfo ="小李"28"60kg"
对象数据类型中,还可以具体的细分: 对象类(Object),数组类(Array),正则类(RegExp),时间类(Date),字符串类(String),布尔类(Boolean),Math数学函数...等对应的实例:对象、数组、正则、时间...
js中对象、类、实例的区别:对象是泛指,js中万物皆对象,类是对对象的具体的细分,实例是类中的一个具体的事物
举例:自然界中万物皆对象,所有的东西可以分为人类、植物类、动物类、物体类,每一个人都是人类中的一个具体的实例
4、基本数据类型和引用数据类型的区别
面试题:
var num1 = 12;var num2 = num1;//把num1代表的值给了num2变量num2++//等于num2 = num2+1 也可以写成num2+=1console.log(num2);//13console.log(num1);//12var obj1 = {name:"小李"};var obj2 = obj1; obj2.name = '小李小李'; console.log(obj1.name)//小李小李console.log(obj2.name)//小李小李
总结:基本数据类型没有跟着改变,引用数据类型跟着改变了
可以看一下下面的图:
基本数据类型和引用数据类型的本质区别: 基本数据类型操作的是值,引用数据类型操作得是对新空间的引用地址
基本数据类型是把值直接的给变量,接下来在操作的过程中,直接拿这个值操作的,可能两个变量存储一样的值,但是互不干扰,其中一个改变,另一个没有任何的影响。
引用数据类型:
1)、定义一个变量
2)、开辟一个新的空间,然后把属性名和属性值保存在这个空间中,并且有一个空间地址
3)、把空间的地址给了这个变量,变量并没有存储这个数值,而是存储的是对这个空间的引用
4)、接下来把这个地址,又告诉给了另外一个变量,另外一个变量存储的也是这个地址,此时两个变量操作的是同一个空间
5)、其中一个改变了空间的内容,另外一个也跟着改变了
5、检测数据类型的方式:typeof instanceof constructor Object.prototype.toString.call()
typeof用来检测数据类型的:typeof 要检查的值
返回值:是一个字符串,包含了数据类型字符"number"、"string"、“boolean”、“undefined”、“object”、"function"
typeof null 的结果是“object”
typeof的局限性:不能具体检查object下细分的类型,检查这些返回的都是“object”
面试题:
console.log(typeof typeof typeof [])// "string" 出现两个以上的typeof 最终结果都是“string”
The above is the detailed content of Detailed explanation of variables and data types in js. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Summary of the system() function under Linux In the Linux system, the system() function is a very commonly used function, which can be used to execute command line commands. This article will introduce the system() function in detail and provide some specific code examples. 1. Basic usage of the system() function. The declaration of the system() function is as follows: intsystem(constchar*command); where the command parameter is a character.

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

Learn Pygame from scratch: complete installation and configuration tutorial, specific code examples required Introduction: Pygame is an open source game development library developed using the Python programming language. It provides a wealth of functions and tools, allowing developers to easily create a variety of type of game. This article will help you learn Pygame from scratch, and provide a complete installation and configuration tutorial, as well as specific code examples to get you started quickly. Part One: Installing Python and Pygame First, make sure you have

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Title: Learn the main function in Go language from scratch. As a simple and efficient programming language, Go language is favored by developers. In the Go language, the main function is an entry function, and every Go program must contain the main function as the entry point of the program. This article will introduce how to learn the main function in Go language from scratch and provide specific code examples. 1. First, we need to install the Go language development environment. You can go to the official website (https://golang.org
