0
var b=4; //variable
document.write("The value  of b is %d", b);

In Java, it would be:

int b;`
System.out.printf("the value of b is %d", b);
TheEagle
  • 5,808
  • 3
  • 11
  • 39
davejimm
  • 1
  • 1
  • 1
    `console.log` instead of `document.write`? [console: Using string substitutions](https://developer.mozilla.org/en-US/docs/Web/API/Console#Using_string_substitutions) – adiga Jan 27 '21 at 13:08
  • There are a variety of ways to do this i'm sure, but my preferred way is Template Literals https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals – TKoL Jan 27 '21 at 13:10
  • what do you mean (exactly) by ""format specifier" for JavaScript? – Mark Schultheiss Jan 27 '21 at 13:14
  • see if this gives you enough information: https://stackoverflow.com/q/43660720/125981 – Mark Schultheiss Jan 27 '21 at 13:21
  • 1
    Does this answer your question? [JavaScript equivalent to printf/String.Format](https://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format) – Mark Schultheiss Jan 27 '21 at 13:22
  • You can use C-like string formatting as in your example. The problem is you need `console.log` rather than `document.write`. Change that and it will work! It's not a very common way of manipulating strings in JavaScript though. Here's some more info https://riptutorial.com/javascript/example/14972/formatting-console-output – Geraint Anderson Jan 27 '21 at 13:25
  • Thank you all, but it seems I haven't gotten exactly what I want. I will keep on studying. – davejimm Jan 27 '21 at 13:53

1 Answers1

1

Try doing this:

let b = 4;

let valueString = `The value of B is: ${b}`
pmsnunes
  • 11
  • 1