Home >Web Front-end >JS Tutorial >How to determine whether it is a string in javascript
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.
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!