Reputation: 109407
While doing a code review of some Javascript files, my code review tool was showing "�" (missing character symbol) in one of the files. Upon further investigation, it turned out that the developer had used non-breaking spaces in their code. (Probably a result of copy/pasting from web.)
To my surprise, this works fine in Chrome, IE11, and Edge. But I'm concerned that it wouldn't work in all browsers. Is this a required feature of Javascript language that I can rely on? Or is this something I need to tell the developer to fix to avoid potential issues on other browsers?
Here is an example:
//NOTE: All spaces in below code are non-breaking space (0xA0), not space (0x20)
function test() {
var mystr = 'hello';
alert(mystr);
}
test();
Upvotes: 5
Views: 369
Reputation: 20228
Yes, no-break spaces are considered whitespace according to the ECMAScript® 2015 Language Specification:
Code Point: Name: Abbreviation: ------------------------------------------------------------------ U+0009 CHARACTER TABULATION <TAB> U+000B LINE TABULATION <VT> U+000C FORM FEED (FF) <FF> U+0020 SPACE <SP> U+00A0 NO-BREAK SPACE <NBSP> U+FEFF ZERO WIDTH NO-BREAK SPACE <ZWNBSP> Other (Zs) Any other unicode "Separator, space" <USP>
Zs refers to code points listed in the "Separator, space" (Zs) category by Unicode 5.1. ECMAScript.
Example code featuring all kinds of whitespace:
// var keyword followed by different kinds of whitespace:
var tab = " ";
varvt = "";
varff = "";
var sp = " ";
var nbsp = " ";
varzwnbsp = "";
var usp = " ";
console.log("whitespace:", tab, vt, ff, sp, nbsp, zwnbsp, usp);
... is this something I need to tell the developer to fix to avoid potential issues on other browsers?
I recommend using spaces only (some allow tabs) as it prevents all kinds of whitespace mangling by different text editors and readability issues as demonstrated by above code sample.
Upvotes: 7