Table of Contents
1 Keywords and Reserved Words" >1 Keywords and Reserved Words
2 Data types" >2 Data types
typeof" > typeof
The relationship between null and undefined" > The relationship between null and undefined
Number type" >Number type
Floating point value" >Floating point value
数据范围" >数据范围
 NaN" > NaN
string" >string
 字符字面量" > 字符字面量
字符串的特点" >字符串的特点
转换为字符串" >转换为字符串
4 Object类型" >4 Object类型
5 JavaScript中+的作用" >5 JavaScript中+的作用
6 javascript中-的作用" >6 javascript中-的作用
7 相等操作符" >7 相等操作符
8 函数" >8 函数
Home Web Front-end JS Tutorial Things JavaScript beginners don't know (2)

Things JavaScript beginners don't know (2)

Mar 15, 2018 pm 03:04 PM
javascript beginner

In this article, we mainly share with you the basic syntax and data types of javascript. Of course, it is still the most basic, but I don’t want to go into details. I hope that students who are just getting started should take a look at it. Otherwise, after seeing this title, I think it is not about syntax and What about data types? With just a little knowledge, I already knew it.

1 Keywords and Reserved Words

ECMA-262 describes a set of keywords with specific purposes. These keywords can be used to indicate the beginning or end of a control statement, or to perform a specific action, etc. As a rule, keywords are also language reserved and cannot be used as identifiers. The following are all the keywords of ECMAScript (the ones with * superscript are new keywords in the 5th edition):

##forswitchdebugger*##default in
break do instanceof typeof
case ##else new var
catch finally return void
continue ##while
function this with
if throw delete
try
##

ECMA-262 also describes another set of reserved words that cannot be used as identifiers. Although reserved words do not yet have any specific use in the language. But they may be used as keywords in the future. The following are all reserved words defined by ECMA-262 version 3:

##boolean exportinterfacestaticbyteextendslongsuper##charclassconst
##abstract enum int short
final native synchronized
##float package throws
goto private transient ##debugger
implements protected volatile double
import public
## Version 5 reduces the reserved words when running in non-strict mode to the following:

class

enumextendssuperconstexportimport

In strict mode, version 5 also imposes restrictions on the following reserved words:

##

Note that let and yield are new reserved words in the 5th edition; other reserved words are defined in the 3rd edition. In order to ensure compatibility to the greatest extent, it is recommended that you use the reserved words defined in the 3rd edition plus let and yield as a reference when programming.

Using keywords as identifiers in JavaScript engines that implement ECMAScript 3 will cause "Identifier Expected" errors. Using reserved words as identifiers may or may not result in the same error, depending on the specific engine.

The 5th edition has slightly modified the rules for using keywords and reserved words. Keywords and reserved words, although still not available as identifiers, can now be used as object property names. In general, it is best not to use keywords and reserved words as identifiers and property names to maintain compatibility with future ECMAScript versions.

In addition to the reserved words and keywords listed above, ECMA-262 5th Edition imposes restrictions on eval and arguments. In strict mode, these two names cannot be used as identifiers or attribute names, otherwise an error will be thrown.

2 Data types

There are five simple data types (also called basic data types) in ECMAscript: Undefined .Boolean, String, Null, Number. There is also a complex data type (sometimes called a reference data type) - Object. Object is essentially a set of unordered key-value pairs. ECMAscript does not support any mechanism for creating custom types, and all values ​​are one of the six data types mentioned above.

typeof

This operator (not a function here, because it is not a function) is generally used during debugging. There are six return values: undefined (value is definition), boolean (Boolean value), string (string), number (number), object (this value is an object or null), function (this value is a function).
Corresponding to the regular expression, it is actually another form of object (RegExp type object), but for versions before Chrome7 and Safari5, function is returned and other browsers return object
Let's take a look at a simple example

var msg; //如果没有赋值,默认是undefinedtypeof msg;  //undefined// age 没有定义typeof age; //undefined
Copy after login

This result is logically reasonable. Because although these two variables are essentially different from a technical point of view, in fact, it is impossible for those variables to perform real operations.
Even if uninitialized variables will automatically be assigned undefined, it is still a wise choice to initialize variables explicitly. If this can be done, then when the typeof operator returns undefined, we can be sure This variable has not been declared yet

The relationship between null and undefined

console.log(undefined == null)  // trueconsole.log(undefined === null)  // false
Copy after login

In fact, the undefined value is derived from null. This is because ECMA-262 stipulates that their equality must return true.
Although null and undefined have such a relationship, their uses are completely different. There is no need to set the value of a variable to undefined under any circumstances, but the same lady rule is inappropriate for null. I think that as long as the object variable is intended to be saved but the real variable has not been saved, the variable should be explicitly saved as null. This can not only reflect the convention of null as a null pointer, but also further distinguish null and undefined

Number type

Number type should be The most concerning data type in ECMAscript, this type uses IEEE754 to represent integers and floating point values

var num1 = 070;  //表示八进制数var num2 = 079   //无效的八进制数  表示79var num3 = 0x1f  //十六进制数
Copy after login

Octal literals are invalid in strict mode. Will cause the javascript engine to report an error

Floating point value

<span style='font-family: 微软雅黑, "Microsoft YaHei";'>var f1 = 0.1; <br>var f2 = .1;  //虽然是有效的0.1但是不推荐var f3 = 1.0; //会自动转换为1</span>
Copy after login

For some very large or very small values, scientific notation can also be used. ,

var f4 = 3.125e7; //31250000
Copy after login

在这里还是要提示一下众所周知的浮点数不能比较相等法则,
在javascript中浮点数的最高精度是17位的小数,但在进行算术计算的时候精确度远远不如整数,例如0.1+0.2不等于0.3而是等于0.0000000000000004(4e-17)
这是使用基于IEEE754数据浮点计算的通病

数据范围

由于内存的限制,ECMAscript并不能保存世界上所有的数,能保存的数能在浏览器中显示出来(通过Number.MAX_VALUE),最小值是(Number.MIN_VALUE)
当超出这个范围的时候,大于就变为Infinity,小于就变为-Infinity

<span style='font-family: 微软雅黑, "Microsoft YaHei";'>var big = Number.MAX_VALUE + Number.MAX_VALUE;<br>console.log(big);  //Infinity</span>
Copy after login

可以使用isFinite()函数判断这个值是否在最大值和最小值之间,如果在就返回true
通过访问Number.NEGATIVE_INFINITY和Number.POSITIVE_INFINITY来得到-Infinity和Infinity

 NaN

NaN,非数值类型(Not a Number)是一个特殊的值,这个数值表示一个应该返回数值的情况结果没有返回数值,在ECMAscript中,任何数值除以0并不会导致错误,会返回一个NaN,因此并不会影响代码的运行。
Nan本身有两个特点:任何设置NaN的操作都会返回NaN,这个特点在多步计算的时候可能导致错误,其次Nan和任何数值都不想等,包括本身:

<span style='font-family: 微软雅黑, "Microsoft YaHei";'>console.log(NaN == Infinity)<br>console.log(NaN == 1)<br>console.log(NaN == NaN)<br>console.log(NaN == 1/0)//以上结果返回的都是false//下面是判断数据类型isNaN(NaN)  //trueisNaN('123') //false,进行了数据类型的转换isNaN('f5') //trueisNaN(4)   //false</span>
Copy after login

运用isNaN()函数可以判断这个变量是否是NaN类型的。
最神奇的是isNaN也是支持对象操作的,在基于对象调用isNaN函数时,会首先调用对象的valueOf方法,然后确定该方法返回的值是否可以转换为数值,如果不能就基于这个返回值再调用toString()方法,在测试返回值

string

对应于string类型,讨论的会比较少一点,string类型用于友零或者多个16位的Unicode字符组成的字符序列,即字符串。js中的单引号和双引号是没有区别的(想对于PHP来讲)。

 字符字面量

转义的只是在这里就不多说了,
字面量在这里只说一点:
var text = "this is \u30a3"这个的长度是9因为\u30a3转义后表示一个字符

字符串的特点

字符串在我们平常的开发当中我们是一直在用的,但是很少人知道其中的原理,下面我们就来讨论一下字符串的特点。
ECMAscript中的字符串是不可变的,也就是说,字符串一旦被创建,他们的值就不能被改变。要改变某个变量保存的字符串就先要销毁原来的字符串,然后用一个新的值来填充该变量,例如下面的代码:
var lang = 'java';
lang = lang + 'script';
以上的代码是变量lang中包含字符串‘java’。而第二行代码把lang的值重新定义为‘java’与’script’的结合,实际上这个操作过程如下:
1. 首先创建一个能容纳10个字符的新字符串
2. 然后将这个字符串中填充’java’和’script’
3. 最后一步是销毁原来的’java’字符串和’script’字符串,因为这两个字符串已经没用了

转换为字符串

在这里主要注意的是null和undefined是没有toString()方法的,如果想要把他们转换为字符串可以使用转型函数String(),能把任何类型的值转换为字符串var val = null;String(val)
当使用数值转换为字符串的时候,可以在toString()传递一个参数,表示把数值变为多少进制的字符串:var num = 10;num.toString(2)返回的是1010;

4 Object类型

ECMAscript中对象其实就是一组数据和功能的集合。
三种实例化一个对象的方法:

var o = new Ocject();var o = new Object;//不推荐使用var o = {};//当今最常用的
Copy after login

object中的对象和属性在后面我们在去讨论

5 JavaScript中+的作用

这个我们在这里说的具体一点
1. 加法运算

console.log(1+2)
Copy after login
  1. 字符串的连接

<span style='font-family: 微软雅黑, "Microsoft YaHei";'>var a ="121",<br>    b =56,<br>    c ="ss";<br>console.log(a+b);//12156console.log(a+c);//121ss</span>
Copy after login
  1. 数值和字符串之间的转换

<span style="font-family: 微软雅黑, "Microsoft YaHei";">//字符串转换我数值var a = "21212";<br/>+a   //21212var b = "ss";<br/>+a //NaNvar c= "0xff";<br/>+a  //255</span>
Copy after login
<span style="font-family: 微软雅黑, "Microsoft YaHei";">//数值转换为字符串var a =45;<br/>+a;  //"45"</span>
Copy after login
//布尔值的转换+true  //1+fasle  //0
Copy after login

6 javascript中-的作用

减号的作用一般是都是用在正规的操作(也就是类型相同)的操作中,信息量虽然不是很大,但是还是有很多需要注意的地方,下面来举几个例子

var a = 5-true;  //4 ,true被转换了var b = NaN -1;//NaNvar c= 5-2   //3var d = 5-"2";//3  “2”被转换为2var e = 5-null; //5  null被转换为了0
Copy after login

7 相等操作符

在我们编程的时候,当使用比较的时候我们希望,有的时候类型不同可以数值一样就可以就好像(2和"2")是可以相等的,但是有时候我们希望要求数值和类型都是一样的才返回真。这就很矛盾了,在ECMAscript中的解决方案是提供两组操作数:相等和全等
相等:先转换在比较
列出一下比较特殊的比较结果

implements package public interface
private static let protected
yield


表达式
null == undefinedtrue
“NaN” == NaNfalse
NaN == NaNfalse
NaN != NaNtrue
true == 2false
undefined == 0false
null == 0false

全等:仅比较不转换
全等就比较严格了,在这里就不讨论什么了。
注意:null == undefined会返回true,但是null === undefined会返回false,这是由于他们的类型不一样

8 函数

函数对于任何语言来说都是一个核心的概念。
在这里主要说一个问题,细节会在后面继续讨论
我们都是知道在函数中的arguments表示所有传进来的参数,比如说

<span style="font-family: 微软雅黑, "Microsoft YaHei";">function foo(a,b,c){<br/>    console.log(arguments[2] === c);//true}</span>
Copy after login

这个原因并不因为他们指向的是同一块内存,因为他们内存空间的值会相互同步,
arguments的值是不能被改写的如果改写的会操作

相关推荐:

JavaScript初学者不知道的事(一)

javascript初学者实践使用

给JavaScript初学者的一些最佳实践

The above is the detailed content of Things JavaScript beginners don't know (2). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Become a C expert: Five must-have compilers recommended Become a C expert: Five must-have compilers recommended Feb 19, 2024 pm 01:03 PM

From Beginner to Expert: Five Essential C Compiler Recommendations With the development of computer science, more and more people are interested in programming languages. As a high-level language widely used in system-level programming, C language has always been loved by programmers. In order to write efficient and stable code, it is important to choose a C language compiler that suits you. This article will introduce five essential C language compilers for beginners and experts to choose from. GCCGCC, the GNU compiler collection, is one of the most commonly used C language compilers

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

C++ or Python, which one is more suitable for beginners? C++ or Python, which one is more suitable for beginners? Mar 25, 2024 am 10:54 AM

C++ or Python, which one is more suitable for beginners? In this era of information technology sweeping the world, programming ability has become an essential skill. In the process of learning programming, choosing a suitable programming language is particularly important. Among many programming languages, C++ and Python are two popular choices for beginners. So, which one is more suitable for beginners, C++ or Python? The following will compare the advantages and disadvantages of the two in various aspects, and why choosing a certain language is more helpful for beginners to get started with programming.

Pandas Beginner's Guide: HTML Table Data Reading Tips Pandas Beginner's Guide: HTML Table Data Reading Tips Jan 09, 2024 am 08:10 AM

Beginner's Guide: How to Read HTML Tabular Data with Pandas Introduction: Pandas is a powerful Python library for data processing and analysis. It provides flexible data structures and data analysis tools, making data processing simpler and more efficient. Pandas can not only process data in CSV, Excel and other formats, but can also directly read HTML table data. This article will introduce how to use the Pandas library to read HTML table data, and provide specific code examples to help beginners

A must-read for beginners: How to choose the appropriate Django version according to your needs? A must-read for beginners: How to choose the appropriate Django version according to your needs? Jan 19, 2024 am 08:20 AM

For beginners, choosing the appropriate Django version is an important and must-face issue. As an efficient web framework, Django has a large number of users and developers, so it also has multiple versions to meet the needs of different products and applications. But how do you choose the right Django version based on your project needs? Below we will use some examples to help you choose the version that suits you. Confirm that the database used Django supports multiple databases, including MySQL, Postgre

See all articles