javascript format code example

Example 1: javascript string format

//

const firstName = 'john';
const lastName = 'smith';

const output = `name: ${firstName}, surname: ${lastName}`;
// name: john, surname: smith

Example 2: f string javascript

`string text ${expression} string text`

Example 3: javascript format date

var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var today  = new Date();

console.log(today.toLocaleDateString("en-US")); // 9/17/2016
console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016
console.log(today.toLocaleDateString("hi-IN", options));

Example 4: format string javascript

const string = 'This is a string.';
const message = `${string} This is also a string.`;

Example 5: javascript full date as string

<p>The toDateString() method converts a date to a date string:</p>

<p id="demo"></p>

<script>
var d = new Date();
document.getElementById("demo").innerHTML = d.toDateString();
</script>
//output: Fri Oct 16 2020