Javascript string update doesn't change the string itself [duplicate] - javascript

This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 1 year ago.
I have following simple javascript code:
s="hello"
s[0]="X"
console.log(s[0])
The output is h, but I have updated it as X, I would ask why I got such result, thanks.

Strings in JavaScript are primitives, which means they are immutable, ie. you are not able to change them. If you were running your code in strict mode then this would throw an error.
If you wanted to modify this string you would need to effectively create a copy of it, and then assign that to the s variable eg.
s = "X" + s.substring(1)

Related

array-like in Javascript detail explanations [duplicate]

This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Does javascript have a method to replace part of a string without creating a new string?
(4 answers)
Closed 1 year ago.
var a = 'cat' ;
a[0] = 'r' ;
a = 'cat'
Why..??
In case of string although you can access elements by array notation, if you try to change its content it will fail silently i.e. will not throw any error but will not change content either.
Please explain me detail.
Strings are primitive values in javascript, and are therefore immutable. This is just how the language works so there's not much to explain besides that. You can read more about it here!
It is not throwing an error because you're probably not running it in strict mode.
Strings are immutable, in JavaScript only objects and arrays are mutable. You can search for mutable data types in JavaScript in Google.
"A mutable object is an object whose state can be modified after it is created." MDN.
You can read more here: Mutable

javascript interpolation using saved variables [duplicate]

This question already has answers here:
Template literals like 'some ${string}' or "some ${string}" are not working
(7 answers)
Closed 1 year ago.
I have a variable saved as object_id in javascript.
I want to target a data attribute by interpolating the object id in to the data attribute.
For example:
$('[data-object-id="${object_id}"]')
However this is not finding the attribute. The are no errors.
I've tried interpolating using '+' but found it quite fiddly in this instance.
For interpolation in string you need to use backticks(`) string. Try:
$(`[data-object-id="${object_id}"]`)

Get value from JSON tree but with a number in id ('300px') [duplicate]

This question already has an answer here:
Using dot notation with number passed into function
(1 answer)
Closed 3 years ago.
I would like to get a picture link that is in my JSON tree:
I'm coding in javascript, and I'm trying to grab that data with:
data.response.cases[1].image.300px
But I have the error (In firefox) :
SyntaxError: identifier starts immediately after numeric literal
I have tried like that:
data.response.cases[i-1].image[0]
But the result is 'undefined'
Have you any idea of how to get this link ?
Thanks
First, "cases" is an object, not an array.
Second, the property "300px" starts with a number, therefore it has the be accessed using the following syntax:
data.response.cases['0'].image['300px'];

JS arrays - advanced assignment [duplicate]

This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 6 years ago.
This is related to the arrays in Javascript, which I am trying to use in a complex logic.
Consider the following code:
a['a1'] = 'AJA'
We know that, this is same as a.a1 = 'AJA' (provided proper definitions were given).
So, if we go ahead and interpret this:
console.log(a.a1[0])
console.log(a.a1[1])
console.log(a.a1[2])
console.log(a.a1)
It logs :
A
J
A
AJA
Now, all I need is to assign a new character at the 4th position.
When I try a[a1][3] = 'Y' or a.a1[3] = 'Y' and then try console.log(a.a1), It still displays AJA instead of AJAY.
I know that we can do this using string concatenation, i.e.
a['a1'] = a['a1'] + 'Y' and get this accomplished.
But why wasn't the first method working? By what other ways can do this?
Strings are immutable. It means that if you create a string, you can't modify it anymore. So your a1 doesn't know anything about 4th character.
You can see this example. I try to change the second char of the already created string, but it will not be changed anymore.
let a = {};
a['a1'] = 'AJA';
a.a1[1] = 'A';
console.log(a.a1);
For more you can see MDN Documentation
As I know a[a1][3] or a.a1[3] is a string variable, you can treat it as:
var s = 'ss';
When you evaluate s[0] you'll get a string value. So when you assign any string value to s, you'll not get 'ss' + anyvalue but anyvalue instead. :)

How to mutate this in a new String function? [duplicate]

This question already has answers here:
Are JavaScript strings immutable? Do I need a "string builder" in JavaScript?
(10 answers)
Closed 7 years ago.
I am still getting used to Javascript (I'm coming from C++) and would like to add a new function to String which mutates the string instance directly. Let's say that I want to add a new character at the midpoint of the string (ignoring any error checking). In C++ you could do something like this->value = .... Is that the way to do this in Javascript? TIA
String.prototype.mutateSelf = function(param1) {
// How do I mutate this specific string instance?
return this;
};
Javascript strings are immutable. You must construct a new string and return it.

Categories