Examples of JavaScript Data Types
All Contributions
Passing a function as argument to other function
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Function Passed as Argument to Other Function</title>
</head>
<body>
<script>
function createGreeting(name){
return "Hello, " + name;
}
function displayGreeting(greetingFunction, userName){
return greetingFunction(userName);
}
var result = displayGreeting(createGreeting, "Peter");
document.write(result); // Output: Hello, Peter
</script>
</body>
</html>
Output :
Hello, Peter
Function data type
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Function Data Type</title>
</head>
<body>
<script>
var greeting = function(){
return "Hello World!";
}
// Check the type of greeting variable
document.write(typeof greeting) // Output: function
document.write("<br>");
document.write(greeting()); // Output: Hello World!
</script>
</body>
</html>
Output :
function
Hello World!
Array data type
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Array Data Type</title>
</head>
<body>
<script>
// Creating arrays
var colors = ["Red", "Yellow", "Green", "Orange"];
var cities = ["London", "Paris", "New York"];
// Printing array values
document.write(colors[0] + "<br>"); // Output: Red
document.write(cities[2]); // Output: New York
</script>
</body>
</html>
Output :
Red
New York
Undefined data type
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript NaN</title>
</head>
<body>
<script>
document.write("Some text" / 2);
document.write("<br>");
document.write("Some text" / 2 + 10);
document.write("<br>");
document.write(Math.sqrt(-1));
</script>
</body>
</html>
Output :
undefined
Hello World!
total contributions (13)
The typeof operator
Output :
number
number
number
number
number
string
string
string
boolean
boolean
undefined
undefined
undefined
object
object
function