侧边栏壁纸
博主头像
云BLOG 博主等级

行动起来,活在当下

  • 累计撰写 318 篇文章
  • 累计创建 6 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录
WEB

JS 最佳判断字符串是否为空和不为空

Administrator
2024-05-10 / 0 评论 / 0 点赞 / 7 阅读 / 0 字

为空

// 简写
function isEmptyStr(s) {
	return s == null || s === '';
}
// 方法二
function isEmptyStr(s) {
	return s == undefined || s === '';
}

function isEmptyStr(s) {
	if (s == null || s === '') {
		return true
	}
	return false
}
// 或
function isEmptyStr(s) {
	if (s == undefined || s === '') {
		return true
	}
	return false
}

不为空

// 简写
function isNotEmptyStr(s) {
	return typeof s == 'string' && s.length > 0;
}

// 不为空
function isNotEmptyStr(s) {
	if (typeof s == 'string' && s.length > 0) {
        return true
	}
	return false
}

0

评论区