Home  >  Article  >  Web Front-end  >  How to determine whether it is a character in javascript

How to determine whether it is a character in javascript

藏色散人
藏色散人Original
2021-06-15 10:52:384912browse

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.

How to determine whether it is a character in javascript

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn