3 Example(s) of JavaScript Data types


Description :

DataType is a very important thing in any programming language because without this computer can't understand what to do with that statement? In this example, we have 5 variable with different values and JavaScript will treat them as string. See the code snippet:


JavaScript Data types Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>   
<body>
<p id="sample"></p>
<script>
var carName = "Audi A8";
var busName = 'Volvo XC60';
var stat1 = "It's alright";
var stat2 = "He is called 'Johnny'";
var stat3 = 'He is called "Johnny"';

document.getElementById("sample").innerHTML =
carName + "<br>" + 
busName + "<br>" + 
stat1 + "<br>" + 
stat2 + "<br>" + 
stat3;
</script>

</body>
</html>

Output

Description :

In this example, We have multiple variable with different datatype like we have 34.45 and 24 and 123e5. basically javaScript start reading the text from left to right so the position of values also matters. See the code snippet:


JavaScript Data types Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>   
<body>
<p id="sample"></p>
<script>
var a1 = 34.45;
var a2 = 24;
var a3 = 24.00;
var x = 123e5;
var y = 123e-5;
document.getElementById("sample").innerHTML = a1 + "<br>" + a2 + "<br>" + a3 + "<br>" + x + "<br>" + y;
</script>
</body>
</html>

Output

Description :

In this example, we have two boolean variable with value true and false and we are concatenating both the variable with "
" tag which is why it will show output as true false


JavaScript Data types Example - 3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>   
<body>
<p id="sample"></p>
<script>
var x = true;
var y = false;
document.getElementById("sample").innerHTML =x + "<br>" + y;
</script>
</body>
</html>

Output