Home >Web Front-end >JS Tutorial >How to determine whether it is a character in javascript
How to judge whether it is a character in JavaScript: 1. Judge by "typeof(str)=='string'"; 2. Use the js native function "Object.prototype.toString" to judge.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
js determines whether it is a string
determines whether it is a string:
1 Basic method:
typeof(str)=='string'
Simple and nothing What to say
2 Use js native functions:
Object.prototype.toString // ƒ toString() { [native code] } Object.prototype.toString.call(str)=="[object String]"
(1) Execute toString in this (context) of Object.prototype The native function will print out the environment variable type inside.
Object.prototype.toString() -->执行结果-->"[object Object]"
(2) If we change this (context), we can print out the current environment variable type and judge based on this type.
Object.prototype.toString.call(str) -->执行结果-->"[object String]"
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to determine whether it is a character in javascript. For more information, please follow other related articles on the PHP Chinese website!