Is non-breaking space always treated as a space in a Javascript source?

While looking at the code of some Javascript files, my code verification tool showed "" (a missing character character) in one of the files. After further investigation, it turned out that the developer used inextricable spaces in his code. (Probably the result of copying / pasting from the Internet.)

To my surprise, this works fine in Chrome, IE11, and Edge. But I am worried that it will not work in all browsers. Is this a required feature of the Javascript language that I can rely on? Or is this what I need to tell the developer to fix potential problems in 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();
Run codeHide result
+4
1

, 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 , ", " (Zs) Unicode 5.1. ECMAScript.

:

// 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);
Hide result

... , , , ?

( ), , .

+6

Source: https://habr.com/ru/post/1678279/


All Articles