Advanced JavaScript(2)

The guide

  1. JavaScript data type
  2. Variable declaration
  3. Type conversion
  4. Add, delete, change and check objects

JavaScript data type

Javascript is divided into 7 data types: 1.number 2.string 3. Boolean Boolean value 4.symbol 5.undefined empty 6.null null 7.object Conclusion: a simple memory method is: the four base (number, string, Boolean, symbol) two empty (undefined, null) an object (object), which is important to note: arrays, functions, date, they don’t belong to the data type, all belong to the object. Each of these data types is described below


Octal :0123 Hexadecimal :0x3F Binary :0b11 Special values: positive 0 and negative 0 Infinity: Infinity + Infinity – Infinity unexpressible number: NaN, the full name of NaN, is not a number. You might think that NaN === NaN, but that’s not the case. There’s no way to tell what these two non-numbers are, so they’re not equal.

console.log(NaN= = =NaN//false

Copy the code

In JavaScript, numbers are stored as floating-point numbers, which means that the decimal point of a number moves around. So how do we represent the largest and the smallest? The following code is used to show the reader

console.log(Number.MAX_VALUE) / / 1.7976931348623157 e+308

console.log(Number.MIN_VALUE) //5e-324

Copy the code

String written as string:

'string' / / single quotation marks

"string" / / double quotation marks

`string` // Backquotes (key under ESC)

Copy the code

Strings are written with single, double, and backquotes, which are not part of the string. Escape character:

\n // indicates a line break

\r // Indicates enter

\t // represents the TAB character

Copy the code

The length of the string can be obtained by string.length

console.log('string'.length); / / 6

Copy the code

Boolean: Boolean Is a simple Boolean that has only two values,true and false, and is used for judgment purposes.

Boolean(undefined); //false

Boolean(null); //false

Boolean(0); //false

Boolean(NaN); //false

Boolean(' '); //false

Copy the code

Null: Undefined and null are both null values, but the two null values are different: 1. If a variable is declared but not assigned, the default value is undefined, not null. 2. If a function does not write return, the default return is undefined instead of null. 3. In general, the null value of a non-object is written to undefined, and the null value of an object is written to NULL.


Definition: an unordered set of key-value pairs of data.

let obj = {'name''bobo'.'age'18}

let obj = new Object({'name''bobo'})

Copy the code

Details: The key name is a string, not an identifier. It can contain any character. The quotation mark can be omitted. You can use variables as attribute names, for example:

let p1 = 'name';

let obj = {[p1]: 'bobo'}// The property name is name

Copy the code

Two kinds of contrast:

  1. Attribute names without [] are automatically changed to strings.
  2. If [] is added, it is evaluated as a variable.
  3. A value that is not a string automatically becomes a string.

Symbol Symbol: indicates a unique value. This is a new syntax in ES6.

let sym = Symbol("test");

console.log(sym);   // Symbol(test)

let sym2 = Symbol("test");

console.log(stm === stm2);   // false

Copy the code

Although the values of the two symbols are the same, they are by no means equal.

Variable declaration

There are three JavaScript declarations :var,let, and const. Let,const is a more useful declaration for ES6. An example of this code is as follows

var a = 1;

let a = 1;

const a = 1;

Copy the code

I’ll cover only the characteristics of these declarations here, focusing on lets and const.


Var declarations: This is the way they were created when JavaScript was first created. Var declarations were created without the concept of block scope, so var exposes variables to global use and promotes them. I won’t spend a lot of time to introduce it here. Interested students can go to MDN to check.


Let declaration: Features:

  1. Improved block scope of var, follow block scope, use scope can not be beyond {} defined area.
  2. Unrepeatable declaration.
  3. It can be assigned or not assigned at all.
  4. You must declare it before using it; otherwise, an error will be reported.
  5. Globally declared let variables do not become window properties.

Const statement:

  1. Has the concept of block scope.
  2. Once created, the value cannot be modified and is fixed.
  3. You cannot duplicate the declaration.

Type conversion

number => string

var n = 5;

console.log(typeof String(n)); //'string'

console.log(typeof (n + ' ')) //'string'

Copy the code

string => number

var s = '5';

console.log(typeof Number(s)); //'number'

console.log(typeof (s - 0)); //'number'

console.log(typeof (+s)); //'number'

console.log(typeof parseInt(s)); //'number'

Copy the code

x => boolean

var s = 1;

console.log(typeof Boolean(s)); //'boolean'

console.log(typeof(!!!!! s);//'boolean'

Copy the code

Add, delete, change and check objects

Delete the properties

delete obj.xxx

delete obj['xxx']

Copy the code

In the preceding two ways, you can delete the XXX attribute of OBJ from undefined and without the attribute name. Method of judgment without attribute name:

'xxx' in obj === false

Copy the code

Contains the attribute name, but the value is undefined

'xxx' in obj && obj.xxx = undefined;

obj.xxx === undefined // cannot determine whether 'XXX' is an obj attribute

Copy the code

View the properties

Object.keys(obj) // View all keys

Object.values(obj) // View all attribute values

Object.entries(obj) // View all key+ values

obj.hasOwnProperty('toString'// Determine whether an attribute is self or common

Copy the code

This is an API that allows you to view the specific key and value of an object. You can also view the value of a specific attribute name in two ways:

obj['key'// Parenthesis syntax, remember that the parenthesis is a string

obj.key // Dot syntax, also a string

Copy the code

Modify and add attributes directly to assignments

let obj = {name'bobo'};

obj.name = 'bobo';

obj['name'] = 'bobo';

obj[name] = 'bobo'// Name is passed in as a variable

obj['na' + 'me'] = 'bobo';

Copy the code

Batch assignment

Object.assign(obj, {age:18.name:'bobo'}); // Assign age and name directly to obj

Copy the code

conclusion

This article focuses on the introduction of JavaScript data types, the basic use of objects, add, delete, change and check, as well as type conversion, this article designed some ES6 syntax for readers to reference learning, I hope you can learn something from this article. Remember to keep learning and follow up! Come on!