Does the value have a length attribute?

I typed 78.length directly into the browser console and entered enter to report an error, but

var someValue = 78;
var strLength = someValue.length;
console.log(strLength);

this does not report an error, but only outputs undefined,. Why?

Apr.07,2021

everything is an object. The Number type inherits from Number.prototype,Number.prototype and inherits from Object.prototype, so if you get the length attribute of number value, you will look for it on Number.prototype and Object.prototype, but cannot return undefined

.

so why does 78.length report an error? Let's talk about the mechanism of . . In js, . immediately after the integer will be understood as a decimal point, so 78.length will not report an error, then 78.0.length will not report an error, 78..length will not report an error, (78) .length will not report an error, and saving the value in a variable to access the attribute will not report an error.


clipboard.png


Number type has no length attribute. You can refer to MDN document Number type description


without length attribute

.
Menu