search
HomeWeb Front-endJS TutorialSummary of JavaScript types, values ​​and variables_javascript skills

Foreword: JavaScript data types are divided into two categories: primitive types and object types. 5 primitive types: number, string, Boolean value, null (empty), undefined (undefined). An object is a collection of attributes, and each attribute is composed of a "name/value pair" (the value can be a primitive value or an object). Three special objects: global objects, arrays, and functions. The core of the JavaScript language also defines three useful classes: date (Date) class, regular (RegExp) class, and error (Error) class.

 1 Number

JavaScript does not distinguish between integer values ​​and floating point values. JavaScript can recognize decimal integer literals (the so-called literals are data values ​​directly used in the program), and hexadecimal values ​​(prefixed with 0x or 0X, which is the number 0 and not the letter o. Think about it if it is the letter o If so, then the hexadecimal value becomes an identifier.) Although the ECMAScript standard does not support octal literals, some implementations of JavaScript can use octal form to represent integers (prefixed with the number 0). The author uses octal to represent an integer in the three browsers of IE, Chrome, and FF on my computer. Variable assignment is no problem either. However, in the strict mode of ECMAScript6, octal literals are explicitly prohibited.

There are two ways to write floating point literals. ① Traditional way of writing real numbers: It consists of an integer part, a decimal point and a decimal part; ② Exponent counting method: that is, the real number is followed by the letter e or E, followed by a positive or negative sign, and then followed by an integer exponent.

 1.1 Overflow of arithmetic operations

Arithmetic operations in JavaScript will not report errors when overflowing, underflowing, or being divisible by 0.

Overflow: When the operation result exceeds the upper limit of the number that JavaScript can represent, the result is positive infinity or negative infinity-Infinity. The behavioral characteristics of infinity values ​​are also consistent with reality: the results of addition, subtraction, multiplication and division operations based on them are still infinite values ​​(of course retaining their signs); underflow: when the operation result is infinitely close to zero and is smaller than the smallest value that JavaScript can represent What happened when the value was still small. In this case, 0 will be returned. The special value "negative zero" is returned when a negative number underflows. Negative zero and whole zero are essentially equal (can even be tested using strict equality ===), except as divisors:

var zero = 0;  //正零值
var negz = -0;  //负零值
zero === negz  //表达式返回值为true
1/zero === 1/negz  
//表达式返回值false,等价于判断正无穷大和负无穷大是否严格相等

Divisibility by 0 will return positive infinity or negative infinity. But dividing 0 by 0 will return NaN (the value of the NaN property of the JavaScript predefined object Number). There are four situations where NaN is returned: ① 0 divided by 0 ② infinity divided by infinity ③ performing a square root operation on any negative number ④ when arithmetic operators are used with operands that are not numbers or cannot be converted to numbers.

There is something special about the NaN value: it is not equal to any value, including itself. There are two ways to judge whether a variable x is NaN: ① Use the function isNaN() ② Use x != x to judge. The expression result is true if and only if x is NaN. There is a similar function isFinite() in JavaScript, which returns true when the parameter is not NaN, Infinity or -Infinity.

 1.2 Binary floating point numbers and rounding errors

There are countless real numbers, but JavaScript can only represent a limited number of them in the form of floating point numbers. In other words, when using real numbers in JavaScript, they are often just an approximate representation of a real value. JavaScript uses IEEE-754 floating point number representation, which is a binary representation that can accurately represent fractions such as 1/2, 1/8, and 1/1024, but decimal fractions 1/10, 1/10 etc. cannot be expressed accurately. For example:

var x = 0.3 -0.2;  //x=0.09999999999999998
var y = 0.2 - 0.1;  // y=0.1
x == y       //false
x == 0.1      //false
y == 0.1      //true
0.1 == 0.1     //true
var z = x + y;   //z=0.19999999999999998

  2 文本

  2.1 字符串、字符集

  字符串(string)是一组由16位值组成的不可变的有序序列,每个字符通常来自于Unicode字符集。字符串的长度(length)是其所含16位值得个数。JavaScript通过字符串类型来表示文本。注意:JavaScript中并没有表示单个字符的“字符型”。要表示一个16位值,只需将其赋值给字符串变量即可。

  JavaScript采用UTF-16编码的Unicode字符集,JavaScript字符串是由一组无符号的16位值组成的序列。那些不能表示为16位的Unicode字符则遵循UTF-16编码规则——用两个16位值组成一个序列(或称作“代理项对”)表示。这意味着一个长度为2的JavaScript字符串有可能表示一个Unicode字符。注意:JavaScript定义的各式字符串的操作方法均作用于16位值,而非字符,且不会对代理项对做单独处理。书看到这里,又结合http://www.alloyteam.com/2013/12/js-calculate-the-number-of-bytes-occupied-by-a-string/上面所述,终于对Unicode字符集、UTF-8、UTF-16稍有理解。

  字符串的定界符可以是单引号或者双引号。这两种形式的定界符可以嵌套,但是不能多层嵌套(比如,双引号可以包含单引号,这时单引号中不能再包含双引号了)。正如上篇所说,一个字符串值可以拆分为数行,每行必须以反斜线(\)结束,这时反斜线和行结束符都不算是字符串内容,即字符串本身并非是多行,只是写成了多行的形式。

  注意:①在JavaScript中字符串是固定不变的(除非重新赋值),类似replace()和toUpperCase()的方法都返回新字符串,原字符串本身并没有变化;②字符串可以当做只读数组,除了使用charAt()方法来查询一个单一字符,也可以使用方括号的方式来访问字符串中的单个字符(16位值),例如:

    s = "hello, world";
    s[0]   //=>"h"

 2.2 Escape character

Escape character meaning
o NUL character (u0000)
b Backspace character (u0008)
t Horizontal tab (u0009)
n newline character (u000A)
v Vertical tab (u000B)
f form feed character (u000C)
r carriage return (u000D)
" Double quotes (u0022)
' Apostrophe or single quote (u0027)
\ Backslash(u005C)
xXX Latin-1 character specified by the two-digit hexadecimal number XX
uXXXX Unicode character specified by 4-digit hexadecimal number XXXX
                                                                                                                                                                                                      Note: If the "" character precedes a character not listed in the table, the "" character is ignored. For example, "#" and "#" are equivalent. Don’t forget that backslashes also have a role in using backslashes at the end of each line in multi-line strings. 

 3 Boolean value

Values ​​in JavaScript can be converted to Boolean values. Among them, null, undefined, 0, -0, NaN, "" (empty string), these 6 values ​​will be converted to false, false and these six values ​​​​are sometimes called "false values"; all other values, including Objects (arrays) will be converted to true, and true and these values ​​are called "true values". Note: Boolean contains the toString() method, so you can use this method to convert a string to "true" or "false", but it does not contain other useful methods.

The above is the entire content of this article, I hope you all like it.

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
The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

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

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

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.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.