JavaScript Learning – Chapter 1 Basic Knowledge (2)

foreword

Tutorial for this article: Modern JavaScript Tutorial

This chapter corresponds to the first part of the tutorial JavaScript Programming Language – JavaScript Basics – 2.4 Variables to 2.5 Data Types

Insert Part 1 JavaScript Programming Language – Miscellaneous – 14.5 BigInt

The development software I use: Visual Studio Code

Variables and Constants

create variable

When creating variables in JavaScript, the let keyword is used. For example, when I create a variable, I write it like this:

 let a; // 这就算是创建好了一个变量,名称为a

Then after the variable is created, we can assign a value to the variable, for example:

 let a; a = 1; // 可以将变量声明和变量赋值整合在一行中,像这样: let a = 1; // 创建好了一个变量,并给这个变量赋值数字1

Multiple variables can be declared on a single line, for example:

 let a = 1, b = 2, c = 3; // 但是为了更好的知道创建变量的具体情况,也就是代码的可读性,最好还是一行只声明一个变量,像这样: let a = 1; let b = 2; let c = 3;

Then, if you want to change the value of the variable, you can reassign it, like this:

 let a; // 创建了一个变量aa = 1; // 此时的变量a的值是1 a = 2; // 此时的变量a的值发生改变,从1更改为了2 // 在此过程中,请注意,不要多次声明同一个变量,这样会导致你的代码出错

The original version of the tutorial introduces the way the old version creates variables, that is, using the var keyword, which will not be introduced here.

After the whole, some keywords of the old version will be introduced separately

variable naming

Like the a above, it is a name for the variable. We create a variable a, which is a process of naming the variable again.
In JavaScript, there are two restrictions on naming variables:

  1. Variable names must contain only letters, number signs $ and underscore _
  2. First letter must be non-digit
  3. If there are multiple words, the camel case naming method is generally used, that is, the words are written consecutively. Except for the first word, the first letter of each remaining word needs to be capitalized, like this:
 let a; // 定义变量a,这种命名方式是有效的let a1; // 定义变量a1,这种命名方式是有效的let variableName; // 驼峰式命名法示例let 1; // 这种命名方式是错误的,不允许以数字开始let var-name; // 这种命名方式是错误的,不允许使用连字符- // 请注意// 1. 区分大小写,也就是说,A 变量和a 变量是不一样的。 // 2. 在JavaScript中,允许非英文字母创建变量,但是不建议。 // 3. 有一张保留字列表,在保留字列表中的内容已经被用作编程语言本身,无法用作变量命名,如let 、class 、return 等。 // 4. 在教程中,有一个未采用use strict 情况下的赋值,讲述在这种情况下可以通过赋值的方式创建变量,此处不做阐述。

Create constants

Use the const keyword to create a constant, it cannot be modified, that is, the value is unchanged, if you change the variable, it will trigger an error:

 const pai = 3.14; // 声明一个pai 常量let pai = 3.1415; // 会触发报错

Constants can be used to record values ​​that are difficult for us to remember and confirm that they cannot be modified later. At this time, we can use capitals and underscores to name these variables, and it is easy to remember these values. And it is easy to use later .

So how are variables named? A variable needs to have a clear, unambiguous meaning to describe the data it contains, and it needs to be concise enough.

If you have a team, then you also need to align your variable term with the team.


At the end of the tutorial, there is a question: reuse a variable or create a new one?

It is definitely better to create a new variable than to waste a lot of time by repeatedly causing the code to fail.

type of data

In JavaScript, there are 8 basic data types, seven primitive types and one reference type.

Number type

Integer and floating point numbers

In addition, there are some special values:

  1. Infinity stands for infinity, and likewise, -Infinity stands for infinitesimal.
  2. NaN stands for computation error, it stands for incorrect or an undefined mathematical operation. Also, if you have a long sequence of computations in your JavaScript script, as long as one part of it is NaN , all subsequent computations will still be NaN . PS: NaN ** 0 results in 1.

Special numeric values ​​are of type “number”. Of course, the common perception of the term “special numbers” is that they are not numbers.

It will be introduced later in Number Types.

BigInt type

In JavaScript, the Number type cannot represent an integer greater than (253-1), or less than -(253-1). This is a technical limitation due to its internal representation (i.e. 9007199254740991).

So we need a type to represent numbers of any length, and this is the BigInt type.

There are two ways to create:

 const bigint = 1234567890123456789012345678901234567890n; // 在整数字面量后面加n const sameBigint = BigInt("1234567890123456789012345678901234567890"); // 调用调用BigInt 函数

It should be noted that since this type is newly added to Javascript, there will be compatibility issues


The content of this divider is from Part 1 JavaScript Programming Language – Miscellaneous – 14.5 BigInt

1. Mathematical operators

  1. In most cases it can be used like a regular numeric type.
  2. It cannot be mixed with regular numeric types. If necessary, you should explicitly use Number() or BigInt() conversion. If the bigint is too large for the numeric type to accommodate, the extra bits will be truncated. And unary addition is not supported.

     > **显式转换** 与**隐式转换** : > > **显式转换**:就是需要使用构造函数,手动转换值的类型。 > > 但是在**显式转换** 的情况下,你可能会遇到精度丢失的情况,并非是四舍五入,而是直接丢失。 > > **隐式转换**:就是系统默认的、不需要加以声明就可以进行转换。
  3. Numbers of type bigint and number can be compared using comparison operators, however, since they are of different types, they may compare equal when == but not when comparing === (strict equality).
  4. In boolean operations, bigint are handled like number .
  5. Polyfill does not elaborate.

String type

Strings must be enclosed in quotes to be used. In JavaScript, there are three ways to use strings:

  1. Double quotes: “Hello”
  2. Single quotes: ‘Hello’
  3. Backticks: `Hello`

The first two are simple quotes, basically no difference, but the third backticks are function expansion quotes, we can use ${…} to embed variables and expressions into strings.

A string can contain zero (empty), one, or more characters.

 let message = "字符串"; // 字符串类型的变量// 嵌入变量alert(`在${messsage}中嵌入一个变量`);

Boolean type (logical type)

Also known as the boolean type, this type contains only two values: true and false , which can be used as the result of a comparison.

“null” value

In JavaScript, the special null value does not belong to any of the above types, it constitutes a separate type that contains only the null value, and is just a special value representing “none”, “null” or “value unknown”.

“undefined” value

In JavaScript, the special value undefined has its own type like null, which means: not assigned a value.

If a variable has been declared but not assigned a value, its value is undefined.

object type and symbol type

It is more complicated in the early stage, and will be mentioned later.

typeof operator

The typeof operator returns the type of the parameter.

A call to typeof x returns the data type as a string.

typeof(x) syntax

You may also encounter another syntax: typeof(x) . It is the same as typeof x .

Simply put: typeof is an operator, not a function. The parentheses here are not part of typeof . It is the parentheses that group the math operations.

Usually, such parentheses contain a mathematical expression, such as (2 + 2) , but here it contains only one parameter (x) . Syntactically, they allow no spaces between the typeof operator and its argument, a style that some people like.

Some people prefer to use typeof(x) , although the typeof x syntax is more common.

This article is reprinted from: https://quest.myxxts.club/archives/144/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment