Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
preface
- Start with javascript basic data types
- Let’s have fun learning!
JavaScript basic data types
- Number Number type
- It is usually an integer
let num = 10 / / decimal
let num = 0xA // hexadecimal 10
let num = 070 // octal 56
typeof num // Number
Copy the code
- Floating point types
let num = 1.1
let num = 0.1
let num = 1. // Equivalent to 0.1
typeof num //Number
Copy the code
- Special existence
let num = (0/0) //NaN
typeof NaN // number means that no number is used to indicate that the numeric operation failed
Copy the code
- String String type
- You can use
' '
and""
Or ‘ ‘to define strings - Strings are immutable and cannot be changed once they are created
- You need to destroy it before you create it
let str = 'hello'
let str = "world"
let str = `vike`
Copy the code
- Boolean Indicates the Boolean type
- There are two Booleans
true
和false
The value converted to true | Convert to a value of false |
---|---|
Non-empty string | An empty string |
Nonzero value | 0,NaN |
Any object | null |
undefined |
- Undefined has only one value and is Undefined
- Use var or let to declare variables undefined if they have no value assigned
// The variable is promoted
console.log(a) //undefined
var a
// let has no variable promotion
let b
console.log(b) //undefined
Copy the code
- Null has only one value that is null
- Null represents a null pointer object
typeof null //object
// undefined is derived from null
undefined= =null //true
Copy the code
- Instances of symbols are unique and immutable
let symbol1 = Symbol(a)let symbol2 = Symbol(a)console.log(symbol1 == symbol2) // false
Copy the code
- BigInt Specifies a value outside the Number range
// The unit of BigInt is n
let num = 2n
typeof num // 'bigint'
Copy the code
JavaScript reference type
- Object Object type
- Creating an object usually uses the object literal notation
let obj = {
a:1.'a':1.3:2
}
Copy the code
- Array Array type
- An ordered set of data
let arr = [1.2.3]
Copy the code
- Function Type
// Function declaration
function fn() {}
// Function expression
let fn = function() {}
// Arrow function
let fn = x= > x
Copy the code
conclusion
- I like to get a thumbs up