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

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

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; // []

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744192350a4562490.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信