Home > Web Front-end > Front-end Q&A > What is blank in javascript

What is blank in javascript

WBOY
Release: 2023-05-26 21:05:37
Original
1054 people have browsed it

In JavaScript, blank is a simple but important concept. It refers to, but is not equivalent to, null and undefined in JavaScript. In this article, we will explore the definition, usage and relationship of blank with null and undefined in detail.

  1. Definition and usage of blank

In JavaScript, we often need to check whether a variable is empty (that is, it has no specified value). In this case we can use blank for comparison. Unlike space, blank is a special JavaScript value used to represent no value. A variable can be set to blank in the following way:

var myVar = null;  // 设置为 null
var myVar;        // 没有指定值,此时 myVar 就是 blank
Copy after login

As you can see, we did not set any value in the second example, and myVar is a blank variable at this time. However, it should be noted that if you want to compare whether a variable is blank, you cannot use the equality operators (== or ===), because they treat blank as undefined, not a "real" value.

The correct way is to use the strict inequality operator (!==), for example:

if (myVar !== null && myVar !== undefined && myVar !== '') {
    // myVar 不是 null、undefined 或空字符串
}
Copy after login

This way you can check whether myVar is a "real" value and avoid blanking it. Incorrectly treated as undefined.

  1. How blank relates to null and undefined

Although blank is similar to null and undefined in a sense, they are not exactly the same. Specifically, blank refers to variables that are not assigned a value, while null and undefined are special values ​​that mean "no value" and "undefined" respectively.

For example, when we define a variable but do not assign a value to it, the variable becomes a blank variable:

var myVar;
Copy after login

And if we explicitly set a variable to null, then it It becomes a null variable:

var myVar = null;
Copy after login

On the contrary, if a variable has not been defined, it is an undefined variable:

// 不存在的变量 myVar
console.log(myVar);  // 输出 undefined
Copy after login

Note that for an undefind variable, you can use the typeof operator to check its type. , but not for blank and null variables.

var myVar;
console.log(typeof myVar);  // 输出 undefined

var myVar = null;
console.log(typeof myVar);  // 输出 object

var myVar;
myVar = '';
console.log(typeof myVar);  // 输出 string

var myVar = undefined;
console.log(typeof myVar);  // 输出 undefined
Copy after login

Summary

In JavaScript, blank is a special way of expressing "no value", which is different from null and undefined. It is used to represent those variables that have not been assigned a value. You can use the strict inequality operator (!==) to check whether a variable is blank. It should be noted that treating blank as undefined and using the equals operator may result in logic errors, so use caution when using it.

The above is the detailed content of What is blank in javascript. For more information, please follow other related articles on the PHP Chinese website!

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