最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What exactly is the empty string ? ( "" ) - Stack Overflow

programmeradmin2浏览0评论

I use this in code from time to time. I was wondering if this is a null character or something similar?

console.log("") returns undefined.

typeof "" returns string.

Object.prototype.toString.call("") returns string.

How is an empty string represented in ascii or unicode? Is the bigger question here.

I use this in code from time to time. I was wondering if this is a null character or something similar?

console.log("") returns undefined.

typeof "" returns string.

Object.prototype.toString.call("") returns string.

How is an empty string represented in ascii or unicode? Is the bigger question here.

Share Improve this question asked Nov 5, 2013 at 16:41 employee-0employee-0 1,0411 gold badge9 silver badges19 bronze badges 1
  • +1 because it is a reasonable question, even if it is ultimately just a misunderstanding of what you're seeing on the console. It is easy to misunderstand though; when you get undefined in this context, it isn't necessarily obvious why. – Spudley Commented Nov 5, 2013 at 16:56
Add a ment  | 

2 Answers 2

Reset to default 5

console.log doesn't return anything, so it's return value is always undefined. There's nothing special about an empty string in that regard.

An empty string does not contain a null character or anything like that. It contains no characters at all. You can think of it a bit like an empty array. It is simply a container (in a sense) which can contain any number of characters, but which happens to be empty.

Yes console.log(""); does return undefined, but this is because log always returns undefined. Above that undefined in your console there should be a blank line, which is the logged empty string. If you use console.warn instead, this empty log is more obvious as it will have a warning sign.

still what is the representation in unicode/ascii

An empty string is zero bytes long. This means it has no representation in these, other than the trivial empty one of how you want to describe it. It will behave in a falsy way.

if ("") 1;
else 0;
// gives 0

If you're representing your characters as an Array of Bytes, for example using a Uint8Array, then

var emptyString = new Uint8Array(0); // length 0
emptyString; // []
发布评论

评论列表(0)

  1. 暂无评论