0

When do we use ` in JavaScript?

hari: for(let i=0; i<=5; i++) { for(let j=0; j<=5; j++) { let value = prompt(`hello(${i},${j})`,''); if (!value) break hari; } } alert('done') //is there any way to use a variable in prompt?

19th Nov 2020, 7:26 AM
Dev Sharma
Dev Sharma - avatar
9 Respuestas
+ 3
Dev Sharma these are called backticks. You can use them instead of quotes whenever you want, they have an advantage, coz we can insert variables right within the string, unlike in quotes, where we have to close the quotes and use + to concatenate. Both examples below would give the same output: 1) alert("Hello " + name + " you are " + age + " years old") 2) alert(`Hello ${name} you are ${age} years old`) I prefer the second one.
19th Nov 2020, 12:55 PM
maf
maf - avatar
+ 1
You can use this way let value = prompt("hello"+your var name+"world");
19th Nov 2020, 7:30 AM
Matias
Matias - avatar
+ 1
Dev Sharma can you give us your code attempt
19th Nov 2020, 12:35 PM
Matias
Matias - avatar
+ 1
Dev Sharma i don't understand, what do you mean by "default value"?
20th Nov 2020, 10:13 AM
maf
maf - avatar
+ 1
well, if it is a variable, and you are not using backticks, then you have to write it without quotes, so "fullname" without quotes.
20th Nov 2020, 10:53 AM
maf
maf - avatar
+ 1
maf thanks
20th Nov 2020, 10:59 AM
Dev Sharma
Dev Sharma - avatar
0
Matiyas Sirak thanks for the answer. But I didn't work.
19th Nov 2020, 11:53 AM
Dev Sharma
Dev Sharma - avatar
0
Thanks maf I understood the second one but I need more clarification in the first one. In first one prompt how Javascript will recognise the default value because we are using four value in double-quotes. Please see below Prompt("hello "+name+"you are "+age+"year old ", "default value");
20th Nov 2020, 9:54 AM
Dev Sharma
Dev Sharma - avatar
0
maf like prompt("please enter your name", "full name"); Here full name is the default value that will appear to the visitor.
20th Nov 2020, 10:36 AM
Dev Sharma
Dev Sharma - avatar