0
How I can use variable in strings?
Hi everyone! I wanna use variable in strings in JavaScript. For Example: var num1 = 487, num2 = 382; var bigger = (num1>num2) ? "num1(How I can use here a variable) is bigger than num2" : "num2 is bigger than num1" document.getElementById ("demo").innerHTML = bigger; I want to print there 487 is bigger than 382! If i change the value i want to change the string also. How can i do it?
4 Réponses
+ 6
Concatenation:
var bigger = (num1 > num2) ? num1 + " is bigger than " + num2 : num2 + " is bigger than " + num1;
+ 5
Option #2
Template literals:
var bigger = (num1 > num2) ? `${num1} is bigger than ${num2}` : `${num2} is bigger than ${num1}`;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
+ 2
Thanks!
+ 1
This is the link for web developers:
https://developer.mozilla.org
Find it here,
The Biggest Website For website Developers!