Javascript String operations - javascript

Javascript has a number of string manipulation operations which can be performed on it. So we have methods like concat(), slice(), match(), etc
My question is do all these string manipulation methods return a new string value as the result OR are there some methods which actually modify the actual string being used in the method ?

Strings in JavaScript (and many other languages) are implemented as immutable objects. This has a few beneficial properties:
It's thread safe, and more specifically,
Multiple references to the same string can be kept safely without having to worry that the value changes.
This also means that all string methods must return a new string if they aim to modify the original value.

The return value is a clone of the original string.
In other words, calling concat, match or slice will not modify the original string.
You can always refer to the MDN reference for documentation
But, to quote from the source
Concat
Combines the text of two or more strings and returns a new string.

Strings are immutable, and once they are created they even cannot be modified again.
Mdn:
Unlike in languages like C, JavaScript strings are immutable. This means that once a string is created, it is not possible to modify it. However, it is still possible to create another string based on an operation on the original string, for example, a substring of the original (by picking individual letters or using String.substr()) or a concatenation of two strings using the concatenation operator (+) or String.concat().
Therefore, yes. Every String operation returns a new String

Related

How to distinguish plain quoted string and template literal(backtick)

They are the same in the result, but they have different abilities. Is there any way to identify between them so that I can restrict the user(of a function) to use only template literal as an argument?
You mean something like an imaginary isFromTemplateLitteral boolean?
function(string){
if(string.isFromTemplateLitteral()){
// console.log("YES!")
}
}
No can do.
Because the string was passed to the function using reference in the memory stack for it. So even before the function body starts running, string is already a normal string. A primitive with type string.
There is no way to know how it has been constructed (template litteral, concatenation, simple/double quotes, etc.) or where it really comes from (user input, harcoded text, regular expression match, etc).
No, not really... just like you can't really differentiate 5 and 4 + 1.

How can I convert a large list of numbers into JavaScript array format?

I have this large list of numbers: http://www.codeshare.io/MGpQ4, that I would like to convert into a JavaScript array format, so that I can check if a certain number in a function matches one of the numbers in that repsective array. Is there any tool I can use to convert all these numbers into a JS array format?
try to split this list. You need to add something like coma or space at the end of your lines.
string.split(/* your delimetter */);
string here is your list in vaid JS format.
If you show real code, i could answer better.
There are many ways to do this, but basically the idea is to replace line breaks by ,. Any IDE that supports regex replacement will allow you to do this easily. You can also use virtually any programming language and use the same approach.
Here's your array literal:
[11450,3082,163222,57748,176966,163225,18786,163224,175522,175523,176967,176968,163226,147285,139762,147317,57793,57784,147324,147333,147336,163220,147337,147338,147339,147340,147341,147342,147343,147192,163175,163351,57847,147202,147211,147214,147215,147284,147216,152398,147218,147219,147220,147221,163397,163424,163425,163399,163427,163400,57749,163401,163402,57875,1070,171146,11452,77475,51933,156790,51920,15615,84544,153564,99754,51946,57916,51919,175521,51973,28016,176957,15620,1067,57929,57881,57882,57883,57885,57884,175524,122321,131411,122320,57886,175525,57887,122322,156789,3075,11462,51581,77411,175528,152719,51580,156801,51568,15648,51583,152553,99735,153797,51582,152554,51567,175529,51586,28017,15652,51587,57974,15628,57917,57918,57919,57920,15630,175526,175527,15629,57927,150956,122340,122341,122342,15633,11467,155198,175658,155201,175654,175655,175656,175697,175657,155200,175653,155199,175652,155203,155202,175640,163147,175648,90635,80913,80914,52762,53369,86207,175649,19255,175650,163129,125474,175651,125473,163130,125475,163131,163132,155344,175641,175642,175643,155345,155346,175644,175645,175646,155348,163148,155349,116724,175647,155347,155350,155351,82161,163149,155240,155241,155242,155243,163141,155244,155245,163142,163143,112424,163144,163145,155246,155247,155248,155249,155250,155251,155252,155253,163146,155254,112425,175638,152360,152361,152362,152363,163139,152364,152365,152366,163140,150666,152367,152368,152369,112428,172007,152345,152352,163137,152353,152354,152355,163138,152356,152357,152358,152359,112426,175639,150663,152340,152341,150664,152342,153004,4250,52357,86647,2993,152537,177649,2994,175626,167902,52365,169271,105860,105408,163582,163584,169272,52373,163583,15665,52382,179239,79720,179194,179240,179241,179242,105387,15662,90634,2996,106113,175627,1060,1059,57990,57991,15687,137084,3002,57988,11483,57989,15689,11510,11511,3001,11484,155183,15690,11507,15691,84434,313,93427,15709,11498,24087,53120,11501,11504,11505,63850,28015,175628,178962,163526,175629,105417,163525,105418,105419,105422,105440,105432,105444,16119,175630,163528,155184,155185,163537,163542,155187,177650,169277,169278,163543,169279,169280,169281,169282,178963,163549,169284,163550,166695,179243,155189,179195,179244,179245,179246,155190,169283,169276,155191,155192,166696,155195,166697,155194,155193,155197,155196,167905,3259,15720,105471,106127,163556,163552,163553,15718,98735,163560,163551,98745,175631,105516,15719,105472,106128,175633,131474,131476,105508,105509,105511,105512,105514,105515,148486,114293,105520,3263,175632,1265,4251,163573,3003,177651,168998,105559,45220,167906,45230,169285,105544,169286,169287,169288,169289,45237,15735,45238,150955,179247,45246,179196,179248,179249,179250,151486,105569,45258,175634,175635,175636,175637,163581,106129,15738,1063,15724,137085,63862,63861,11524,163587,11525,163588,11527,11523,11514,63853,163593,11521,11522,163586,63854,11530,63855,11532,45279,15746,4844,11554,3009,169001,172378,63856,11534,11535,53447,46809,15751,11517,15752,15755,15753,167903,117769,63857,15747,11536,117771,108898,11538,155206,15748,63859,63858,15754,1264,63863,11555,63864,63865,63866,155226,63867,63869,53159,15775,84275,314,169291,63852,169292,169293,169295,80600,169296,169298,169302,42526,163570,169303,3034,95672,53557,45333,55793,53548,62107,11632,63889,175759,175760,163600,163601,163602,163603,163604,163605,20287,11681,163606,163607,175805,20288,175806,74962,74963,74964,74965,74966,74967,74968,167909,962,163608,163609,167910,163610,163611,167911,163612,163613,167912,163614,163615,163616,163617,4116,14062,163618,163619,163620,163621,163622,163623,163624,163625,163626,14065,175807,163627,48559,14070,1464,74969,74970,74971,74972,74973,74974,74975,167913,960,957,163594,163595,163596,163597,163598,163599,167908,20291,175761,52383,52384,167914,52385,52386,52387,52388,155227,52389,52390,167915,52391,52392,52393,52394,155228,52395,52396,167916,52397,52401,52398,52399,155229,52400,52402,167917,52403,52404,52405,155230,52406,155231,52407,106130,48861,163628,163629,163630,48862,28022,175762,48864,48865,52408,52409,48866,52410,52411,52412,52413,155232,52414,52415,52416,52417,48867,52418,52419,52420,52421,52422,52423,52424,52425,52426,48891,52427,155233,48908,52438,52428,48881,48868,52429,167918,52430,52431,52432,52433,52434,52435,52436,52437,48886,167919,48892,48897,48902,155235,48909,52439,52440,48882,48869,52441,48887,166698,52442,52443,52444,52445,52446,52447,52448,52449,167920,48893,48898,48903,155236,48910,52450,52451,48883,48870,52452,48888,166699,52453,52454,52455,52457,52458,52459,52460,52461,167921,48894,48899,48904,155238,48911,155239,106131,965,74976,74977,74978,74979,74980,74981,74982,167922,48561,125472,156949,156950,125476,155234,125477,155237,156952,156951,170065,91247,41964,45051,41965,45052,45053,50975,50977,50976,52535,163631,41966,45054,50978,50979,50980,45055,50981,50982,50983,50985,50986,52536,50987,52537,50988,50989,50990,50991,50992,50993,50994,41967,45056,45057,14879]
It seems that you plan to use your array to check the existence of some values. In this case, you might be better off with an object map which will perform O(1) lookups.
{"313":true,"314":true,"957":true,"960":true,"962":true,"965":true,"1059":true,"1060":true,"1063":true,"1067":true,"1070":true,"1264":true,"1265":true,"1464":true,"2993":true,"2994":true,"2996":true,"3001":true,"3002":true,"3003":true,"3009":true,"3034":true,"3075":true,"3082":true,"3259":true,"3263":true,"4116":true,"4250":true,"4251":true,"4844":true,"11450":true,"11452":true,"11462":true,"11467":true,"11483":true,"11484":true,"11498":true,"11501":true,"11504":true,"11505":true,"11507":true,"11510":true,"11511":true,"11514":true,"11517":true,"11521":true,"11522":true,"11523":true,"11524":true,"11525":true,"11527":true,"11530":true,"11532":true,"11534":true,"11535":true,"11536":true,"11538":true,"11554":true,"11555":true,"11632":true,"11681":true,"14062":true,"14065":true,"14070":true,"14879":true,"15615":true,"15620":true,"15628":true,"15629":true,"15630":true,"15633":true,"15648":true,"15652":true,"15662":true,"15665":true,"15687":true,"15689":true,"15690":true,"15691":true,"15709":true,"15718":true,"15719":true,"15720":true,"15724":true,"15735":true,"15738":true,"15746":true,"15747":true,"15748":true,"15751":true,"15752":true,"15753":true,"15754":true,"15755":true,"15775":true,"16119":true,"18786":true,"19255":true,"20287":true,"20288":true,"20291":true,"24087":true,"28015":true,"28016":true,"28017":true,"28022":true,"41964":true,"41965":true,"41966":true,"41967":true,"42526":true,"45051":true,"45052":true,"45053":true,"45054":true,"45055":true,"45056":true,"45057":true,"45220":true,"45230":true,"45237":true,"45238":true,"45246":true,"45258":true,"45279":true,"45333":true,"46809":true,"48559":true,"48561":true,"48861":true,"48862":true,"48864":true,"48865":true,"48866":true,"48867":true,"48868":true,"48869":true,"48870":true,"48881":true,"48882":true,"48883":true,"48886":true,"48887":true,"48888":true,"48891":true,"48892":true,"48893":true,"48894":true,"48897":true,"48898":true,"48899":true,"48902":true,"48903":true,"48904":true,"48908":true,"48909":true,"48910":true,"48911":true,"50975":true,"50976":true,"50977":true,"50978":true,"50979":true,"50980":true,"50981":true,"50982":true,"50983":true,"50985":true,"50986":true,"50987":true,"50988":true,"50989":true,"50990":true,"50991":true,"50992":true,"50993":true,"50994":true,"51567":true,"51568":true,"51580":true,"51581":true,"51582":true,"51583":true,"51586":true,"51587":true,"51919":true,"51920":true,"51933":true,"51946":true,"51973":true,"52357":true,"52365":true,"52373":true,"52382":true,"52383":true,"52384":true,"52385":true,"52386":true,"52387":true,"52388":true,"52389":true,"52390":true,"52391":true,"52392":true,"52393":true,"52394":true,"52395":true,"52396":true,"52397":true,"52398":true,"52399":true,"52400":true,"52401":true,"52402":true,"52403":true,"52404":true,"52405":true,"52406":true,"52407":true,"52408":true,"52409":true,"52410":true,"52411":true,"52412":true,"52413":true,"52414":true,"52415":true,"52416":true,"52417":true,"52418":true,"52419":true,"52420":true,"52421":true,"52422":true,"52423":true,"52424":true,"52425":true,"52426":true,"52427":true,"52428":true,"52429":true,"52430":true,"52431":true,"52432":true,"52433":true,"52434":true,"52435":true,"52436":true,"52437":true,"52438":true,"52439":true,"52440":true,"52441":true,"52442":true,"52443":true,"52444":true,"52445":true,"52446":true,"52447":true,"52448":true,"52449":true,"52450":true,"52451":true,"52452":true,"52453":true,"52454":true,"52455":true,"52457":true,"52458":true,"52459":true,"52460":true,"52461":true,"52535":true,"52536":true,"52537":true,"52762":true,"53120":true,"53159":true,"53369":true,"53447":true,"53548":true,"53557":true,"55793":true,"57748":true,"57749":true,"57784":true,"57793":true,"57847":true,"57875":true,"57881":true,"57882":true,"57883":true,"57884":true,"57885":true,"57886":true,"57887":true,"57916":true,"57917":true,"57918":true,"57919":true,"57920":true,"57927":true,"57929":true,"57974":true,"57988":true,"57989":true,"57990":true,"57991":true,"62107":true,"63850":true,"63852":true,"63853":true,"63854":true,"63855":true,"63856":true,"63857":true,"63858":true,"63859":true,"63861":true,"63862":true,"63863":true,"63864":true,"63865":true,"63866":true,"63867":true,"63869":true,"63889":true,"74962":true,"74963":true,"74964":true,"74965":true,"74966":true,"74967":true,"74968":true,"74969":true,"74970":true,"74971":true,"74972":true,"74973":true,"74974":true,"74975":true,"74976":true,"74977":true,"74978":true,"74979":true,"74980":true,"74981":true,"74982":true,"77411":true,"77475":true,"79720":true,"80600":true,"80913":true,"80914":true,"82161":true,"84275":true,"84434":true,"84544":true,"86207":true,"86647":true,"90634":true,"90635":true,"91247":true,"93427":true,"95672":true,"98735":true,"98745":true,"99735":true,"99754":true,"105387":true,"105408":true,"105417":true,"105418":true,"105419":true,"105422":true,"105432":true,"105440":true,"105444":true,"105471":true,"105472":true,"105508":true,"105509":true,"105511":true,"105512":true,"105514":true,"105515":true,"105516":true,"105520":true,"105544":true,"105559":true,"105569":true,"105860":true,"106113":true,"106127":true,"106128":true,"106129":true,"106130":true,"106131":true,"108898":true,"112424":true,"112425":true,"112426":true,"112428":true,"114293":true,"116724":true,"117769":true,"117771":true,"122320":true,"122321":true,"122322":true,"122340":true,"122341":true,"122342":true,"125472":true,"125473":true,"125474":true,"125475":true,"125476":true,"125477":true,"131411":true,"131474":true,"131476":true,"137084":true,"137085":true,"139762":true,"147192":true,"147202":true,"147211":true,"147214":true,"147215":true,"147216":true,"147218":true,"147219":true,"147220":true,"147221":true,"147284":true,"147285":true,"147317":true,"147324":true,"147333":true,"147336":true,"147337":true,"147338":true,"147339":true,"147340":true,"147341":true,"147342":true,"147343":true,"148486":true,"150663":true,"150664":true,"150666":true,"150955":true,"150956":true,"151486":true,"152340":true,"152341":true,"152342":true,"152345":true,"152352":true,"152353":true,"152354":true,"152355":true,"152356":true,"152357":true,"152358":true,"152359":true,"152360":true,"152361":true,"152362":true,"152363":true,"152364":true,"152365":true,"152366":true,"152367":true,"152368":true,"152369":true,"152398":true,"152537":true,"152553":true,"152554":true,"152719":true,"153004":true,"153564":true,"153797":true,"155183":true,"155184":true,"155185":true,"155187":true,"155189":true,"155190":true,"155191":true,"155192":true,"155193":true,"155194":true,"155195":true,"155196":true,"155197":true,"155198":true,"155199":true,"155200":true,"155201":true,"155202":true,"155203":true,"155206":true,"155226":true,"155227":true,"155228":true,"155229":true,"155230":true,"155231":true,"155232":true,"155233":true,"155234":true,"155235":true,"155236":true,"155237":true,"155238":true,"155239":true,"155240":true,"155241":true,"155242":true,"155243":true,"155244":true,"155245":true,"155246":true,"155247":true,"155248":true,"155249":true,"155250":true,"155251":true,"155252":true,"155253":true,"155254":true,"155344":true,"155345":true,"155346":true,"155347":true,"155348":true,"155349":true,"155350":true,"155351":true,"156789":true,"156790":true,"156801":true,"156949":true,"156950":true,"156951":true,"156952":true,"163129":true,"163130":true,"163131":true,"163132":true,"163137":true,"163138":true,"163139":true,"163140":true,"163141":true,"163142":true,"163143":true,"163144":true,"163145":true,"163146":true,"163147":true,"163148":true,"163149":true,"163175":true,"163220":true,"163222":true,"163224":true,"163225":true,"163226":true,"163351":true,"163397":true,"163399":true,"163400":true,"163401":true,"163402":true,"163424":true,"163425":true,"163427":true,"163525":true,"163526":true,"163528":true,"163537":true,"163542":true,"163543":true,"163549":true,"163550":true,"163551":true,"163552":true,"163553":true,"163556":true,"163560":true,"163570":true,"163573":true,"163581":true,"163582":true,"163583":true,"163584":true,"163586":true,"163587":true,"163588":true,"163593":true,"163594":true,"163595":true,"163596":true,"163597":true,"163598":true,"163599":true,"163600":true,"163601":true,"163602":true,"163603":true,"163604":true,"163605":true,"163606":true,"163607":true,"163608":true,"163609":true,"163610":true,"163611":true,"163612":true,"163613":true,"163614":true,"163615":true,"163616":true,"163617":true,"163618":true,"163619":true,"163620":true,"163621":true,"163622":true,"163623":true,"163624":true,"163625":true,"163626":true,"163627":true,"163628":true,"163629":true,"163630":true,"163631":true,"166695":true,"166696":true,"166697":true,"166698":true,"166699":true,"167902":true,"167903":true,"167905":true,"167906":true,"167908":true,"167909":true,"167910":true,"167911":true,"167912":true,"167913":true,"167914":true,"167915":true,"167916":true,"167917":true,"167918":true,"167919":true,"167920":true,"167921":true,"167922":true,"168998":true,"169001":true,"169271":true,"169272":true,"169276":true,"169277":true,"169278":true,"169279":true,"169280":true,"169281":true,"169282":true,"169283":true,"169284":true,"169285":true,"169286":true,"169287":true,"169288":true,"169289":true,"169291":true,"169292":true,"169293":true,"169295":true,"169296":true,"169298":true,"169302":true,"169303":true,"170065":true,"171146":true,"172007":true,"172378":true,"175521":true,"175522":true,"175523":true,"175524":true,"175525":true,"175526":true,"175527":true,"175528":true,"175529":true,"175626":true,"175627":true,"175628":true,"175629":true,"175630":true,"175631":true,"175632":true,"175633":true,"175634":true,"175635":true,"175636":true,"175637":true,"175638":true,"175639":true,"175640":true,"175641":true,"175642":true,"175643":true,"175644":true,"175645":true,"175646":true,"175647":true,"175648":true,"175649":true,"175650":true,"175651":true,"175652":true,"175653":true,"175654":true,"175655":true,"175656":true,"175657":true,"175658":true,"175697":true,"175759":true,"175760":true,"175761":true,"175762":true,"175805":true,"175806":true,"175807":true,"176957":true,"176966":true,"176967":true,"176968":true,"177649":true,"177650":true,"177651":true,"178962":true,"178963":true,"179194":true,"179195":true,"179196":true,"179239":true,"179240":true,"179241":true,"179242":true,"179243":true,"179244":true,"179245":true,"179246":true,"179247":true,"179248":true,"179249":true,"179250":true}
Like plalx said, any IDE with regex replacement can do it.
As an alternative, you can chech http://www.regexr.com/
And use something like /([0-9]+)/g for matching those numbers and then use a substitution like $1,
Then just add your [].
In your Excel spreadsheet, create a cell with a function that pulls all the numbers in the list into a javascript array format. Something like:
=CONCATENATE("[", TRANSPOSE(A1:A700 & ”, “) ,"]")
Something like that should create a new cell with all your list values in a single, comma delimited string, enclosed by [].
Copy the entire string and place it in your javascript file as a variable.
var list = [value1, value2, value3, ...];
Then you can do whatever you want with it.

Alternative syntax for splitting a JavaScript string into an array of characters?

I wanted to split a string into individual characters for output, and I thought about using string.split("") to convert my string into an array, but I found that it wasn't necessary. Look at this:
<script type = "text/javascript" language = "Javascript">
var vx = "FGHIFKL"
for (var i = 0; var i<vx.length; i++)
{
document.write(vx[i]); // Outputs FGHIFKL
}
</script>
It seems you can access strings as if they were arrays by using vx[0] without explicitly using
str.split. Because vx[0] = F, vx[1] = G, etc. Is this syntax generally allowed, or even recommended for use in browsers? And how come this even works? I didn't know you could do something like this until today. Thanks in advance!
Yup, you can use [] notation to pull individual characters out of JS strings.
Keep in mind that this doesn't work in the older IE's (7 definitely fails, and I believe 8 does as well).
You can use charAt for a cross-browser solution
"asdf".charAt(1) === "s";
A String in JavaScript is a sequence of characters.
There are two ways to get to individual characters in a string.
The charAt method: str.charAt(n)
ECMAScript 5 introduced a way to treat strings as an array-like object: str[n]
On the other hand, split method returns a new array which is an array of strings separated into substrings. Interesting to note is the fact that if separator parameter to the split method is an empty string, the string is converted to an array of characters.
Reading numerical indicies of strings is a reliable feature of javascript as of ES5. These index properties are not quite the same as on arrays in that they are not writable or configurable. Check out the MDN page for strings and the ECMAScript Language Specification sections 15.5.5 and 15.5.5.2 for more information.

JSON Attribute Best Practice

Usually, what I see is {"attribute":241241}
but writting this does exactly the same thing : {attribute:241241}.
Is {attribute:241241} considered bad practice and should be avoided or not?
{attribute:241241} does not validate using JSONLint, so yes, it is bad practice and should be avoided.
Further, if you look at the JSON spec at json.org, you will find that for objects the first value is always a string (indicated by double quotes).
You are confusing JSON with Object literal syntax, in Javascript doing
var o = {
"attribute":241241
};
is not JSON, but object literal. And yes, the quotes are useless there.
The JSON specification requires that keys be quoted, so the second form isn't valid JSON.
As Snakes and Coffee noted, it is a valid Javascript object literal (on which JSON is based), and some JSON parsers will accept it anyway. So it may work in some cases, but sooner or later you'll run into a perfectly functional JSON parser that rejects your non-quoted keys because they aren't valid per the spec.
As per the specification here, the names in name/value pairs must be of type string.
An object is an unordered collection of zero or more name/value pairs,
where a name is a string and a value is a string, number, boolean,
null, object, or array.

Why doesn't the null-terminator placed arbitrarily in a string terminate it?

The null character or null-terminator (\0) is to be used to terminate a contiguous sequence of characters. I find that in C, I can add the character into a string at a random position and the string will be cut off from that point. For example:
char * s = "Hello\0World";
will result in s being equal to the string "Hello". In JavaScript, however, this is not the case:
var s = "Hello\0World";
The above won't work as expected. s will be equal to the string "HelloWorld".
Why doesn't this work?
JavaScript does not use NULL terminated strings, while C does.
Javascript strings are stored by keeping track of the characters and the length separately instead of trying to assume that a NULL marks the end of the string.
The C string still points to an address in memory where "Hello\0World" is stored, only that most string handling functions considers 0 end of string. For some functions you must pass a string length argument, but most simply read until they find the null byte. In memory the string is actually "Hello\0World\0".
A JavaScript engine cannot determine string length by looking for a null byte, since you in such a case wouldn't ever be able to have a nullbyte inside a string. There's probably something about that in the specs. The engine must instead storing the length of the string separately, and then read that many characters from memory whenever you access the string.
And how to properly parse and store the size of buffers is something scripting languages usually try to hide from the user. That's half the purpose of scripting, to not require the programmer to worry about adding 0 to created character buffers and or storing string length separately so that string handling functions don't print a bunch of random characters outside your buffer looking for a nullbyte...
So exactly how does a JavaScript string behave? I don't know, it's probably up to the engine to describe its properties in depth. As long as you interface with the object like the specification says, it can be implemented in whatever manner, using structs for buffer and length, using a translation character for 0, using a linked list of characters, etc...
In Javascript a NULL byte in a String is simply a NULL byte in a string.
If you want truncate the string
var s = "Hello\0World".split("\0").shift();
but in this case I think it not need to disturb the null byte :)

Categories