I am confused why this code causes the name [0] to be "f" instead of "foo". I thought that when I pass the "separation method", foo will be the result.
var ar = []; ar[0] = "foo bar"; ar[1] = "dumdumdum"; var name = ar[0].split(' '); console.log(name[0]);
The variable is namereserved in browsers. If you must enter the developer console (click f12) and enter window.name, you will see that it either gives you ""or some other string result.
name
window.name
""
The previous code ar[0].split(' ');will return the following array:
ar[0].split(' ');
[ "foo", "bar" ]
, , typeof ;), "foo,bar". .
typeof
"foo,bar"
name[0] "foo,bar" , f
name[0]
f
"name" window.name . .
, array2
var ar = []; ar[0] = "foo bar"; ar[1] = "dumdumdum"; var name2 = ar[0].split(' '); console.log(name2[0]);
name , , JavaScript. name - , belopw.
var name = ar[0].split(' '); .
var name = ar[0].split(' ');
var ar = []; ar[0] = "foo bar"; ar[1] = "dumdumdum"; var changeThisVariableName = ar[0].split(' '); console.log(changeThisVariableName[0]);
Wrap it in a function, you will get exactly the result, because the name refers to window.name
(function () { var ar = []; ar[0] = "foo bar"; ar[1] = "dumdumdum"; var name = ar[0].split(' '); console.log(name[0]); })()
Source: https://habr.com/ru/post/1675391/More articles:macOS Server 5.3 Calendar pg_ctl not starting - synchronizationHow do I know the gem is compatible with the rails version? - ruby | fooobar.comHow to build an array of intervals from other intervals - algorithmFind the most common row or matrix mode of vectors - Python / NumPy - pythonPre-processing function of both continuous and categorical variables (integer type) with scikit-learn - pythonhow to make git log --graph the default value - gitresponse.js application showing 404 not found on nginx server - reactjsIcons / images not displayed with TabBarBottom in React Native - react-nativeБаза данных _replicator не масштабируется, или мой дизайн нуждается в настройке - couchdbWhy is Hugo serving blank pages? - hugoAll Articles