Home >Web Front-end >JS Tutorial >What are primitive values in JavaScript? Detailed explanation of primitive values in JavaScript
The content of this article is about what is the original value in JavaScript? The detailed explanation of original values in JavaScript has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Original value:
Concept: The value of the original type is the original value, that is, the original value has five types: string, number, boolean, null and undefined .
Use of valueOf() and toString() methods:
null and undefined do not have valueOf and toString methods, and an error will be reported when called.
The valueOf method is called by default:
The valueOf method definition of each JavaScript object is different:
For example:
// 函数: <script> var f = function () {} console.log(f) // ƒ () {} console.log(f.toString()) // function () {} console.log(f.valueOf()) // f () {} </script> // 对象 <script> var obj = {a: 1, b: 'ss'} console.log(obj) // {a: 1, b: "ss"} console.log(obj.toString()) // [object Object] console.log(obj.valueOf()) // {a: 1, b: "ss"} </script>
(1) Call the valueOf method of the object. If the return value is the original value, it is returned directly, otherwise the object itself is returned.
(2) For an array, if its toString() method is not overridden, its default implementation is to call the array's join() method return value as the return value of toString().
Example 1:
[1,2,3].toString() // '1,2,3' var str = new String("11") console.log(str) //String {"11"} console.log(str.valueOf()) // 11 (typeof返回string) console.log(str.toString()) // 11 (typeof返回string)
The reason why the return value of str.valueOf() is 11:
The valueOf method here calls the valueOf method on the String prototype, Instead of the valueOf method on the prototype of the Object object, String.prototype is before Object.prototype in the prototype chain structure, and the String.prototype.valueOf method returns a string, so the return value is 11.
Example 2:
<script> var obj = new Object({a: 1}) console.log(obj) // {a: 1} console.log(obj.valueOf()) // {a: 1} console.log(obj.toString()) // [object Object] console.log(obj + 1) // [object Object]1 </script>
Reason: When using objects for operations, the valueOf method is called first. At this time, the return value of valueOf is still an object, so the toString method is called again for operation. Therefore, the return value is [object Object]1.
Determine the specific type of a value:
Use Object.prototype.toString(). call(val)
Return values are:
String | Number | Boolean | Null | Undefined | Function | Array | Object | RegExp | Date | Error | Arguments
and above Just what is the original value in JavaScript? A complete introduction to the detailed explanation of primitive values in JavaScript. If you want to know more about JavaScript Tutorial, please pay attention to the PHP Chinese website.
The above is the detailed content of What are primitive values in JavaScript? Detailed explanation of primitive values in JavaScript. For more information, please follow other related articles on the PHP Chinese website!