Home > Web Front-end > JS Tutorial > body text

How to determine whether it is a string in javascript

青灯夜游
Release: 2023-01-03 09:31:52
Original
26443 people have browsed it

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是字符串类型");
}
Copy after login

Output:

str是字符串类型
Copy after login

[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)
Copy after login

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!