Make a summary every day, persistence is victory!

/** @date 2021-05-29 @description toString() */Copy the code

One (sequence)

ToString can be used as a function of variables and types.

const num = 10; console.log(num.toString(2)); / / '1010'Copy the code

I realized I didn’t know enough about the toString method, so I summarized.

Two (detailed explanation)

String: The value is a String of objects.

const str = new String('test');

console.log(str.toString()); // 'test'
Copy the code

Number: Returns the string form of the Number object; Parameter radix (numeric to string conversion radix (2-36), defaults to 10, RangeError if thrown out of range; If Number is negative, the sign is preserved;

// Const num = 12; console.log(num.toString(2)); // '1100' // The numeric form needs to be wrapped around () console.log(12.tostring (2)); // Uncaught SyntaxError: Invalid or unexpected token console.log((12).toString(2)); / / / / '1100' negative situation, 0 return to '0' console. The log ((1). The toString ()); // '-1' console.log((-0).toString()); / / '0'Copy the code

Boolean: Returns the string form of a Boolean object; When concatenated with a string, the toString method is automatically called;

console.log(false.toString()); // 'false'
console.log('this is ' + true); // 'this is true'
Copy the code

Symbol: Returns the string form of the Symbol object; Unable to concatenate string directly;

console.log(Symbol('a') + 'b'); // Uncaught TypeError: Cannot convert a Symbol value to a string

Symbol('a').toString(); // 'Symbol(a)'
Copy the code

BigInt: is similar to Number, but does not handle the following n; And the toString method can be called directly in its original form, in most cases without ();

// const bigintNum = 12; console.log(bigintNum.toString(2)); // '1100' // in numeric form,-0n needs to wrap console.log(12n.tostring (2)) with (); // '1100' // -0n.toString() is - + 0n.toString(); console.log(typeof -0n.toString()); // 'number' // negative case console.log(-12n.tostring (2)); // '-1100' console.log((-0n).toString()); / / '0'Copy the code

Object: all objects to return to the [Object Object], the call Object. The prototype. ToString. Call (obj) returns [Object type], in which the type is the type of the Object; So you can use Object’s toString method to detect data types;

const obj = {a: 1};
console.log(obj.toString()); // [object Object]

console.log(Object.prototype.toString.call(obj)); // [object Object]
console.log(Object.prototype.toString.call('test')); // [object String]
console.log(Object.prototype.toString.call(1)); // [object Number]
console.log(Object.prototype.toString.call(true)); // [object Boolean]
console.log(Object.prototype.toString.call(null)); // [object Null]
console.log(Object.prototype.toString.call(undefined)); // [object Undefined]
console.log(Object.prototype.toString.call(1n)); // [object BigInt]
const foo = function() {
    return 'foo';
}
console.log(Object.prototype.toString.call(foo)); // [object Function]
console.log(Object.prototype.toString.call([])); // [object Array]
console.log(Object.prototype.toString.call(new Date())); // [object Date]
console.log(Object.prototype.toString.call(/test/)); // [object RegExp]
Copy the code

Array: Returns all the elements of the current Array as a string, separated by a comma. Where null and undefined are converted to ”

console.log(['test', 1, true, null, undefined, {a: 1}, function() {}, new Date()].toString()); // Test,1,true,,,[object object],function() {},Sun May 30 2021 20:06:22 GMT+0800Copy the code

Function: returns a string representing the source code of the current Function; Function. The prototype. The toString () to any parameters are returned to the Function () {} [native code]

function sum(a, b) {
  return a + b;
}

console.log(sum.toString()); // 'function sum(a, b) {
                                   return a + b;
                                 }'
console.log(Function.prototype.toString(1)); // 'function () { [native code] }'
Copy the code

Date: Returns a string in The American English Date format. Concatenating the string automatically calls toString.

console.log(new Date().toString()); // Sun May 30 2021 19:30:26 GMT+0800 (Chinese standard time) console.log('current time: '+ new Date().tostring ()); // Current time: Sun May 30 2021 19:31:04 GMT+0800Copy the code

Three (Attention points)

  1. NumberType is required when the type is called directly(a)Package, otherwise error;
  2. BigIntType in addition to-0nYou need to use(a)Package, no other need, no package-0n.toString()Is actually- + 0n.toString();
  3. In addition toSymbol, all other types automatically call the toString method when concatenated to a string.
  4. In addition toObject, can only handle the same type when calling toString methods on other type prototypes, such as:
Number.prototype.toString.call('test'); // Uncaught TypeError: Number.prototype.toString requires that 'this' be a Number
Copy the code
  1. useObject.prototype.toString.callCan detect all data types;

Iv (Extended)

Implement all data type checking functions:

function typeCheck(val) {
    return Object.prototype.toString.call(val).slice(8, -1);
}

console.log(typeCheck(1)); // "Number"
console.log(typeCheck(true)); // "Boolean"
Copy the code