What does timestamp = + new Date () mean?

How is

timestamp = + new Date () calculated and assigned?

Mar.13,2022

+ variable
implicitly calls the variable's valueOf method and converts it to numeric type

var obj = {
  toString(){
    console.log('toString');
    return '13'
  },
  valueOf(){
    console.log('valueOf');
    return []
  }
}

console.log(+obj)// valueOf toString 13

first call valueOf if the value returned by valueOf is not the original value, it will continue to call toString and convert the string 13 to number 13

.


is equivalent to Date.now () timestamp assignment


  1. The result of new Date () is a Date object
  2. + is a unary operator that changes the following variable to a type of Number , which is equal to Number (new Date ()) .
Menu