There are seven built-in data types in JavaScript

  • number(digital)
  • string(String)
  • boolean(Boolean value)
  • symbol(symbols)
  • null(null)
  • undefined(Undefined)
  • object(object)

A: Number

Special values for numbers are:

  • 0, + 0, 0.

  • Infinity Infinity, plus Infinity +Infinity, minus Infinity -Infinity;

  • Inexpressible number — NaN;

Second, string

1, writing

'hi' / / single quotation marks
"hi" / / double quotation marks
`hi` / / the quotes
Copy the code

Quotes are not part of a string

2, escape

For example, ‘it’s OK ‘, the JS engine will think ‘it’ is over.

Correct term

  • Escape: preceded by a slash, such as ‘it’s OK ‘
"it's ok" // Use double quotation marks

`it's ok` //- use backquotes
Copy the code

Escape — Use a different way to say what you want

  • ‘–‘ single quotes
  • “–” double quotation marks
  • \ n – a newline
  • \ r – press enter
  • \t — TAB character
  • \ b – space
  • \ – slash
  • \uFFFF — The corresponding Unicode character
  • \xFF — The first 256 Unicode characters
  • Multiline strings: Surrounded by backquotes

3. Multi-line strings

  • Type enter in the string
`let s =`You can do this with backquotes`
Copy the code

4. String length

Writing: the string length

For example: ‘\n\r\t’. Length is 3

Characters can be read by subscript (like arrays)

For example, let s=’hello’; S [0] // Value is “h”

5. String concatenation

String + any type = new string after concatenation

As long as there are strings concatenated with other types, the end result is a string type.

1, string “add” eg: ‘hello’ + ‘ ‘+’ world ‘= Hello world

2, value string “add” eg: ‘100’ + ‘100’ = 100100

String + value eg: ’12’ + 12 = 1212

Formula: add values and connect characters

6. Base64 transcoding

(Usually used to hide the resume email in the job Posting)

  • Window. btoa Normal string is converted to base64 encoded string

  • The window.atob base64 encoded string is converted to the original string

Boolean bool (true/false)

The following operators yield bool values

  • No operation

! value

  • Equal to the operation

1 == 2, 1! = 2, 3 == 2, 3 == 3 = = 4

  • Comparison operations

1 > 2, 1 >= 2, 3 < 4, 3 <= 4

The if statement has a bool

if(value){... }else{... }Copy the code

If statements often need to be true or false. What if value is not a bool attribute?

Five falsy value

  • undefined
  • null
  • 0
  • NaN
  • ' '(Empty string)

These five falsy values represent false. (And false)

Undefined and null

Undefined and null are two empty types. The differences are as follows:

Declare a variable but do not assign a value. The default value is undefined, not null.

If the function does not return, the default return is undefined, not null.

Traditionally, null values for non-objects are written as undefined and null values for objects are written as null.

  • undefined Add them to the numerals, and the output is NaN
  • nullAnd the number type, the output is the original number

5. Symbool (rarely used)

What is Symbol in JS?

Variable declaration

Three ways to declare:

var a=1  / / out of date
let a=1  //let is the new, more reasonable way to declare local variables
const a=1  // The declaration must be assigned and cannot be changed
Copy the code

Let declaration and const declaration

  • Follow block scope, that is, use scope beyond {};
  • Cannot repeat the declaration;
  • It may or may not be assigned;
  • You must declare it before using it; otherwise, an error will be reported.
  • Globally declared let variables do not become window properties.

A const declaration is almost the same as a let except that it is assigned and cannot be changed.

Type conversion

1, the number = > string

Var n =1

  • Method one:String(n)
  • Method 2:n + ''(Empty string)

I didn’t change the value of n, I just created a new value

2, stringr = > number

Var s = ‘123’

  • Method one:Number(s)
  • Method 2:s-0
  • Method 3:+s
  • Method 4:parseInt(s) / parseFloat(s)

I didn’t change the value of s, I just created a new value.

3, x => bool

  • Method one: Boolean(x)
  • Method 2:!!!!! x
Boolena(1)  //ture
Boolena(0)  //false!!!!!1   //ture!!!!!0  //false
Copy the code

4, x => string

  • Method 1: String(x)

  • Method 2: x.tostring ()

    Object

Definition: an unordered set of data, a set of key-value pairs

Writing:

let obj = { 'name': 'frank'.'age': 18 }  //
let obj = new Object({'name': 'frank'})  //
console.log({ 'name': 'frank, 'age18}) ':Copy the code
  • Key names are strings, not identifiers, and can contain any character
  • Quotation marks can be omitted, after which only identifiers can be written
  • Even if quotes are omitted, the key is still a string (important)

How do I know that the key is a string?

Keys (obj) to print all keys of obj.

  • Variable attribute name

How do you use variables for property names before you use constants for property names

let a = 'name'
let obj = { a : 'frank'}      // The property is called 'a'
let obj = { [a] : 'frank' }  // The property name is 'name'
Copy the code

contrast

  • Attribute names without [] are automatically changed to strings
  • [] is evaluated as a variable
  • A value that is not a string automatically becomes a string