This question already has answers here:
What does ${} (dollar sign and curly braces) mean in a string in JavaScript?
(6 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 2 years ago.
I have seen ${ } being used in many javascript codes. What exactly does this do?
For example:
updateDisplay(){
if(this.operation != null){
this.previousOperandTextElement.innerText = ${this.previousOperand} ${this.operation};
}
this.currentOperandTextElement.innerText = this.currentOperand;
}
Why would we not use + to concatenate in this case?
It's called a template literal. It achieves the same thing as concatenation but in a more readable manner:
const a = "Hello"
const b = "."
console.log(`${a} World${b}`)
Related
This question already has answers here:
What are the Alternatives to eval in JavaScript?
(11 answers)
Closed 8 months ago.
console.log(Math.sqrt(4));
let str = 'Math.sqrt(4)';
console.log(str);
Is there any way for the 2nd console.log to show 4?
Is there any function which can do this?
You can use eval(), like
let str = eval('Math.sqrt(4)');
To do this, you can use the eval() function to evaluate the code in the string. Like this:
let str = 'Math.sqrt(4)';
console.log(eval(str));
This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Execute JavaScript code stored as a string
(22 answers)
Closed 1 year ago.
i have a string as below and i would like to extract the array between the quotes.
mystring = "['str1','str2']"
I tried it with eval and i do not want to use eval in my code. is there any other neat way to do this ?
function parseString(string) {
return string
.split(",")
.map((str) => str.replace("[", "").replace("]", "").replaceAll("'", ""));
}
This assumes none of array indexes includes character ",".
This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Why do regex constructors need to be double escaped?
(5 answers)
Closed 2 years ago.
I can't seem to find the issue with this code:
const regex = "/tel:\+61(\d+)/g";
const subscriberNumber = senderAddress.match(regex);
For input text, senderAddress = tel:+619123456789 the subscriberNumber is null.
What's causing it to return null?
You haven't escaped the + in 61. + is used as a concatenation operator too.
I don’t think the parentheses around \d+ are necessary.
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 3 years ago.
I am playing with regular expressions in Javascript and have stumbled across an unexpected behaviour. Take a look: https://jsfiddle.net/ft7h5cw0/
I am trying to replace .html-wrap string in a #my-html-wrapper-random .html-wrap .main-content with #my-html-wrapper-random .html-wrap. For some reason, the result after replacement is:
#my#my-html-wrapper-randomper-random .html-wrap .main-content instead of expeceted #my-html-wrapper-random #my-html-wrapper-random .main-content
What is the reason for this behaviour?
var testString = "#my-html-wrapper-random .html-wrap .main-content";
var rxString = ".html-wrap";
var rx = new RegExp(rxString);
var result = testString.replace(rx,"#my-html-wrapper-random"); // unexpexted result strinf
console.log(result);
This question already has answers here:
Double quote in JavaScript string
(4 answers)
Closed 4 years ago.
I am trying to write it like this:
str.replace("'", """)
but it gives me an syntax error, how to properly write this?
use escape str.replace("'", "\"")