html display javascript variable code example

Example 1: html use js variable as text

<h1>"My number: " <span id="myText"></span></h1>

<script>

	//You can use an IIFE (Immediately Invoked Function Expression)
	(() => {
  		var number = "123";
		document.getElementById("myText").innerHTML = number;
	})();
</script>

Example 2: how to print to console javascript

console.log("string")

Example 3: how to print a line in javascript

console.log("Your line would be here")//Prints a line on the console 
alert("Your message here")//Raises an alert

Example 4: how to print the value of variable in javascript in html

var name="kieran";
document.getElementById("output").innerHTML=name;

and the html code would be: 

<p id="output"></p>

Example 5: how to display javascript variable value in html page

// Plain Javascript Example
var $jsName = document.querySelector('.name');
var $jsValue = document.querySelector('.jsValue');

$jsName.addEventListener('input', function(event){
  $jsValue.innerHTML = $jsName.value;
}, false);


// JQuery example
var $jqName = $('.name');
var $jqValue = $('.jqValue');

$jqName.on('input', function(event){
  $jqValue.html($jqName.val());
});

Tags:

Java Example