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?

4th Nov 2018, 10:28 AM
Maksat Orazsahedow
Maksat Orazsahedow - avatar
4 Antworten
+ 6
Concatenation: var bigger = (num1 > num2) ? num1 + " is bigger than " + num2 : num2 + " is bigger than " + num1;
4th Nov 2018, 10:38 AM
Harsh
Harsh - avatar
+ 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
4th Nov 2018, 10:57 AM
ODLNT
ODLNT - avatar
+ 2
Thanks!
4th Nov 2018, 10:47 AM
Maksat Orazsahedow
Maksat Orazsahedow - avatar
+ 1
This is the link for web developers: https://developer.mozilla.org Find it here, The Biggest Website For website Developers!
5th Nov 2018, 1:17 AM
Potato Hacker
Potato Hacker - avatar