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

How to determine whether it is a character in javascript

藏色散人
Release: 2021-06-15 10:52:38
Original
4955 people have browsed it

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'
Copy after login

Simple and nothing What to say

2 Use js native functions:

Object.prototype.toString  // ƒ toString() { [native code] }
Object.prototype.toString.call(str)=="[object String]"
Copy after login

(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]"
Copy after login

(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]"
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template