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

How to determine whether it is a string in javascript

青灯夜游
青灯夜游Original
2021-03-30 17:00:3026624browse

Javascript method to determine whether it is a string: first use the typeof operator to return the type of the uncalculated operand; then use the constructor attribute to return a reference to the Boolean function that created this object; finally pass "== "Just determine whether the two return values ​​​​are strings.

How to determine whether it is a string in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript determines whether it is a string type

var str="123";
if((typeof str=='string')&&str.constructor==String){
	console.log("str是字符串类型");
}

Output:

str是字符串类型

[Recommended learning: js basic tutorial

Description:

1. typeof operator:

typeof operator returns a string, indicating uncalculated The type of operand.

The syntax is as follows (typeof operator followed by operand):

typeof operandtypeof(operand)

Parameters:

  • operand: a representation object or primitive value The expression whose type will be returned.

2. Constructor attribute:

The constructor attribute returns a reference to the Boolean function that created this object.

Grammar:

object.constructor

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of How to determine whether it is a string 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