JavaScript mainly look at ruan Yifeng teacher’s JavaScript tutorial, this stage is going to go through quickly, mainly to record some key points that js is different from OC and Swift

Basic data types

  • Number
  • String (string)
  • Boolean (Boolean)
  • Undefined: undefined or does not exist. That is, there is no value here because there is no definition.
  • Null: indicates that the value is null.
  • Object: A collection of values.

JavaScript objects can be divided into three subtypes:

  1. Object in a narrow sense
  2. Array
  3. Functions

JavaScript treats functions as data types that can be assigned values to variables, which brings great flexibility to programming and lays the foundation for “functional programming” in JavaScript.

The typeof operator

The typeof operator can return a data typeof a value.

typeof 123 // "number"
typeof '123' // "string"
typeof false // "boolean"

// The function returns function
function f() {}
typeof f
// "function"

// undefined returns undefined.
typeof undefined
// "undefined"

// null returns object.# #typeof null // "object"
Copy the code

Null, and undefined

Null typed returns object. Null and undefined can both mean ‘nothing’. This is due to the history of JS design. We only need to understand the main differences between them:

nullIs an object representing "empty", when converted to a value0;undefinedIs a representation"Not defined here"Is the original value of, when converted to valueNaN.// Null calculation
Number(null) / / 0
5 + null / / 5


// undefined count
Number(undefined) // NaN
5 + undefined // NaN


Copy the code

Boolean value

All values are considered true except for the following six values that are converted to false.

  • undefined
  • null
  • false
  • 0
  • NaN
  • “” or “(empty string)