发布时间:2026-07-21阅读(2)
了解多种在 JavaScript 中轻松检查字符串是否为空的方法。

1. 将字符串与空字符串进行比较
要检查 JavaScript 中的字符串是否为空,我们可以在 if 语句中将字符串与空字符串 () 进行比较。
例如:
function checkIfEmpty(str) { if (str === ) { console.log(String is empty); } else { console.log(String is NOT empty); }}const str1 = not empty;const str2 = ; // emptycheckIfEmpty(str1); // outputs: String is NOT emptycheckIfEmpty(str2); // outputs: String is empty
要将仅包含空格的字符串视为空字符串,请在将字符串与空字符串进行比较之前调用该字符串的 trim() 方法。
function checkIfEmpty(str) { if (str.trim() === ) { console.log(String is empty); } else { console.log(String is NOT empty); }}const str1 = not empty;const str2 = ; // emptyconst str3 = ; // contains only whitespacecheckIfEmpty(str1); // outputs: String is NOT emptycheckIfEmpty(str2); // outputs: String is emptycheckIfEmpty(str3); // outputs: String is empty
String trim() 方法删除字符串开头和结尾的所有空格并返回一个新字符串,而不修改原始字符串。
const str1 = bread ;const str2 = milk tea ;console.log(str1.trim()); // breadconsole.log(str2.trim()); // milk tea
小费
在验证表单中的必填字段时修剪字符串有助于确保用户输入实际数据而不仅仅是空格。
如何检查字符串是否为空、null 或未定义
根据您的情况,您可能需要考虑该字符串可能是一个空值(null 或未定义)。 要检查这一点,请直接使用字符串 if 语句,如下所示:
function checkIfEmpty(str) { if (str) { console.log(String is NOT empty); } else { console.log(String is empty); }}const str1 = not empty;const str2 = ; // emptyconst str3 = null;const str4 = undefined;checkIfEmpty(str1); // outputs: String is NOT emptycheckIfEmpty(str2); // outputs: String is emptycheckIfEmpty(str3); // outputs: String is emptycheckIfEmpty(str4); // outputs: String is empty
如果字符串为空或为空,则在 if 语句中将其强制为 false。 否则,它将被强制为真。
要删除所有空格并检查空值,请在 if 语句中使用字符串之前,使用可选的链接运算符 (?.) 调用该字符串的 trim() 方法。
function checkIfEmpty(str) { if (str?.trim()) { console.log(String is NOT empty); } else { console.log(String is empty); }}const str1 = not empty;const str2 = ; // emptyconst str3 = null;const str4 = undefined;const str5 = ; // contains only whitespacecheckIfEmpty(str1); // outputs: String is NOT emptycheckIfEmpty(str2); // outputs: String is emptycheckIfEmpty(str3); // outputs: String is emptycheckIfEmpty(str4); // outputs: String is emptycheckIfEmpty(str5); // outputs: String is empty
可选的链式操作符让我们可以在 null 或未定义的字符串上调用 trim() 方法而不会导致错误。 相反,它会阻止方法调用并返回 undefined。
const str1 = null;const str2 = undefined;console.log(str1?.trim()); // undefinedconsole.log(str2?.trim()); // undefined
2.比较字符串的长度和0
或者,我们可以访问字符串的长度属性并将其值与 0 进行比较,以检查字符串是否为空。
function checkIfEmpty(str) { if (str.length === 0) { console.log(String is empty); } else { console.log(String is NOT empty); }}const str1 = not empty;const str2 = ; // emptycheckIfEmpty(str1); // outputs: String is NOT emptycheckIfEmpty(str2); // outputs: String is empty
要使用这种方法检查仅包含空格的字符串,我们还将在将修剪后的字符串的长度与 0 进行比较之前调用 trim() 方法。
function checkIfEmpty(str) { if (str.trim().length === 0) { console.log(String is empty); } else { console.log(String is NOT empty); }}const str1 = not empty;const str2 = ; // emptyconst str3 = ; // contains only whitespacecheckIfEmpty(str1); // outputs: String is NOT emptycheckIfEmpty(str2); // outputs: String is emptycheckIfEmpty(str3); // outputs: String is empty
关注七爪网,获取更多APP/小程序/网站源码资源!
Copyright © 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图HTML地图XML地图