Note: This article is part 1 of chapter 3 of the “Relearning JavaScript Advanced Programming” series on Data types.
About “relearning JavaScript advanced programming” is a review of the basics of JS learning.

Premise: The core of any language describes the basic working principles of the language, usually involving the syntax, operators, data types, built-in functions, and other basic concepts used to build complex solutions.

1. Grammar

Js variables, function names, and operators are case sensitive, and other keywords such as typeof cannot be usedCopy the code

2. The identifier

An identifier is the name of a variable, function, attribute, or function parameter.

The identifier must start with a letter, underscore (_), or dollar sign (USD). Other characters can be letters, underscores (_), dollar signs, or digits. 3Copy the code

3. Comments

Js comments include single-line comments, which begin with two backslashes, and block-level comments, which begin with (/) and end with (/)

Single line comment // block level comment /* block level comment */Copy the code

4. The statement

In JS statements ending in fractions is not required, but a semicolon is recommended. This is because there are no unnecessary problems when compressing the code.

5. Keywords and reserved words

There are specific keywords in JS that can be used to indicate the beginning or end of a control statement, or to perform specific operations, etc. Keywords are reserved by the language and therefore cannot be used as identifiers. In addition, there are other reserved words in JS that cannot be used as identifiers.

6. The variable

Because JS variables are loosely typed, loosely typed means they can be used to hold any type of data. Each variable is simply a placeholder for the value. Var is used to define variables. In ES6, the let and const keywords were added to define variables.

Var a = 10 // Define a variable a and assign 10 to itCopy the code

Note: Although omitting the var operator can define global variables, it is not recommended because defining global variables in local scope can make code difficult to maintain.

7. data types

There are five basic data types in JS: Undefined, Null, Boolean, Number, and String, and the complex type Object. Object is an unordered set of name-value pairs. There is no mechanism for creating custom types in ES.

8. Typeof operator

The datatype used to test the given variable returns the following values:

Undefined Boolean -- Boolean string -- string number -- value object -- object or null function -- functionCopy the code

The operands of typeof operators can be either variables or numeric literals. Typeof is an operator, not a function.

9. undefined

It has only one special type of value, undefined, which is undefined if the variable is not assigned after var.

var message;
message // undefined
Copy the code

Note: both uninitialized and unassigned variables typeof are undefined, so it is recommended that all values be declared in use in order to better determine whether the source of undefined is unassigned or uninitialized.

10. Null type

Null is the second datatype that has only one value. The value is NULL, which ostensibly represents an empty object pointer. This is why typeof detects that NULL returns “object”.

If you want to define a variable to hold an object, the best thing to do is to assign the variable a value of NULL so that you can detect null to see if the corresponding variable holds a reference to an object.

Note: null == undefined is true, although this is not the same use. There is no need to explicitly set a variable to undefined in any case, whereas NULL is explicitly set to hold null if the variable holding the object does not actually hold the object. This further distinguishes null from undefined.

11. A Boolean type

Boolean types have two literals: true and false, and these two values are not the same as numeric values. True does not necessarily equal 1 and false does not necessarily equal 0.

Note: True and false are case sensitive, true and Flase are just identifiers.

We can convert a value to a Boolean type using the function ** Boolean()**. As follows:

var message = 'haha,nihao';
var zh = Boolean(message)
Copy the code

The rules for each type of conversion are as follows:

The data type The value converted to true Convert to a value of false
Boolean true false
String Any non-empty string “” (empty string)
Number Any non-zero numeric value (including infinity) 0 and NaN
Object Any object null
Undefined Do not apply undefined

12. The number type

Data types widely used in JS, including integer and floating point values. Can be expressed in decimal, octal, hexadecimal literals.

Var BJZ = 070 // octal 56 var SJZ = 56 // decimal 56 var SLJZ = 0xA // hexadecimal 10Copy the code

12.1 Floating Point Value

The value must contain a decimal point and be followed by at least one digit.

Var f = 1.1Copy the code

Note: Since floating point numbers have twice as much memory as integers, they are automatically converted to integers in the following cases:

Var f = 1. // No decimal point is saved as 1 var b = 10.0 // Save as 10Copy the code

In addition, too large or too small values can be expressed by E (scientific notation), as follows:

Var f = 3.125e7 // 3.125 * 10^7 => 31250000 var f = 3e-17 // js converts floating point values with more than 6 zeros after the decimal point to scientific notationCopy the code

Note: 0.1 + 0.2! = 0.3

12.2 Value Range

Due to the limitation of memory, JS cannot store all the values. The minimum value that JS can represent is 5E-324, and the maximum value is 1.7976931348623157 E + 308. If the value of a calculation exceeds the maximum, it is converted to Infinity, and the minimum is -infinity. If these two values are present, the value cannot be evaluated after participation.

12.3 NaN

A non-numeric value in js is a special value. This is used to prevent an error from being thrown when an operand that returns a value does not.

NaN has two specificities: 1. NaN is returned by any operation involving NaN, and 2. NaN is not equal to any value including itself. For both of these features, ES defines the isNaN() function, which takes a parameter of any type and helps us determine if the parameter is “not a value.” isNaN() takes the parameter and tries to convert it to a value. As follows:

// true isNaN(10) // false (10 is a number) isNaN("10") // false (can be converted to number 10) isNaN('blue') // true (cannot be converted to number) IsNaN (true) // flase (can be converted to a value of 1)Copy the code

Note that isNaN() can also be used on objects. When called based on an object, valueOf() is first called to determine whether the return valueOf the method can be converted to a value. If not, the toString() method is called based on the return value, and the return value is tested.

12.4 Numerical Conversion

There are three functions to convert non-numeric values to numeric values: Number(), parseInt(), and parseFloat(). The first function can be used for any data type. The other two functions convert strings to numbers.

The Number() conversion rule is as follows: Boolean value => true or flase => 1 or 0 number => pass and return NULL => return 0 undefined => return NULL string if contains decimal values '123' => 123, '012' => 12' 1.1' => 1.1 if it is a floating point string, '01.1' => 1.1 if it contains hexadecimal '0xf' => same decimal if it is empty '' => 0 'if it contains other than the above format '' => NaN' If it is an object, the valueOf() method of the object is called, converting the return value according to the previous rules, If the conversion object is a NaN, the toString() method of the object is called, returning the string value following the previous rules. ParseInt () is converted as follows: it ignores the space before the string and returns NaN if the first character is not a number or a minus sign. If it is any other base, it is converted to the decimal value of the response. Var m = parseInt('1234blue') => NaN parseFloat() Each character is parsed from the first character until the first invalid floating-point character is encountered which means that the first decimal point in the string is valid and the second decimal point is invalid. So the string after it is ignored. Var m = parseFloat('0xA') => 0 var c = parseFloat('22.21.2') => 22.21 var b = The parseFloat (' 22.4 ') = > 22.4Copy the code

13. Type string

A string that represents a sequence of zero or more 16-bit Unicode characters. Enclosed by double quotation marks (“”) or single quotation marks (“”)

Var a = '124' var b = "123" var a = '124' var b = "123"Copy the code

13.1 Character literals

Some special character literals, namely escape sequences. A character used to represent a non-print character or for some other purpose.

literal meaning
\n A newline
\t TAB
\b The blank space
\r enter
\f In the paper
\ slash
Single quotes
Double quotation marks

These characters can appear anywhere in the string and will be interpreted as a single character.

The length of any string can be obtained using the length attribute.

let b = '1kljk'
b.length // 5
Copy the code

13.2 Characteristics of Strings

Strings are immutable in ES. Once a string is created, its value cannot be changed. To change the value of a string, destroy the original value and reassign it

var lang = 'shanghai'
lang = lang + 'daxue'

// shanghaidaxue
Copy the code

13.3 Converting to a String

There are two ways to convert a value to a string. Pass toString() as follows:

var age = 11;
var agestring = age.toString() // '11'
var b = true
var bstring = b.toString() // 'true'
Copy the code

Most data types (values, booleans, objects, and string values) have toString** methods, but null and undefined do not. In most cases, toString does not need to pass arguments. The default is a string that returns a numeric value in decimal format. But you can actually pass octal, binary, and hexadecimal parameters.

var num = 10;
num.toString()      // '10'
num.toString(2)     // '1010'
num.toString(8)     // '12'
num.toString(10)    // '10'
num.toString(16)    // 'a'
Copy the code

14. The object type

An object is a collection of data and capabilities. Objects can be created by executing the new operator followed by the name of the object type to be created. Creates an instance of type Object to which you can add properties and methods.

var o = new Object()
Copy the code

In ES, the Object type is the basis for all instances of it, and any properties and methods that the Object type has also exist in more concrete objects.

Each instance of Object has the following properties and methods:

Constructor => Save the function used to create the current object

HasOwnProperty (propertyName) => Checks whether the given property exists in the current object instance. PropertyName must be specified as a string.

IsPrototypeOf (object) => Is used to check whether an incoming object is a prototype of another object.

PropertyIsEnumerable (propertyName) => checks if the given property can be enumerated in a for-in statement. As with hasOwnProperty(), the propertyName as an argument must be specified as a string.

ToString () => returns a string representation of the object

ValueOf () => Returns a string, numeric, or Boolean representation of the object. ,

Welcome to my official account [Xiaoyao students]