Does defining a frequently used string as a variable improve performance? [duplicate] - javascript

This question already has answers here:
Do common JavaScript implementations use string interning?
(3 answers)
Closed 7 years ago.
In some languages, frequently used strings are defined as variables/constants, which are called instead of literal strings. Is this the same with JavaScript? In particular, I have frequent use of the string 'none'. Instead of writing the literal 'none' everywhere in the code, would it improve performance if I define:
var none = 'none';
and use none everywhere in the code? Or, is there a way to intern a literal string expression so that it is evaluated only once?

Literal strings are automatically interned by most Javascript compilers. So var a = 'hello' and var b = 'hello' will likely already be pointing at the same copy of the 'hello' string in memory, no need for further optimization on your part.
The only way to make sure different string objects are created for the same string value is by defining each one via the String global object, i.e.:
var a = new String('hello');
var b = new String('hello');

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 string update doesn't change the string itself [duplicate]

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)

JavaScript any way to declare strict type variables [duplicate]

This question already has answers here:
Typesafe Javascript
(8 answers)
Closed 2 years ago.
Hy I am a new to JavaScript. I have done programming with Java. In Java we declares variables with proper type. string, int, char etc. Is there a way to declare variable in JavaScript having proper type instead of let, var etc.
While using javascript stack (javascript, typescript) you are going to use let/const but you can either strictly define the type like:
let a: number = 5;
or leave it to javascript engine
let b = 5;
Javascript is a dynamically typed language, the type is assigned by the javascript itself, it has some ways of assigning types to variables, but none natively, they all need some framework.
The most famous way is typescript which is a superset of javascript, other way is flow.js.
If you want only check a variable type you can use typeof or === operator. e.g: == check only value, 1 == "1" is true, but === check value and type, 1 === "1" is false.
If you're new to JS I personally recommend focus in learn JavaScript and in future study other frameworks such as Typescript.
useful study links
https://www.w3schools.com/js/
https://developer.mozilla.org/en-US/docs/Web/JavaScript

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