Examples of JavaScript Data Types

0

All Contributions

The typeof operator

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JavaScript typeof Operator</title>
</head>
<body>
    <script>
    // Numbers
    document.write(typeof 15 + "<br>");  // Prints: "number"
    document.write(typeof 42.7 + "<br>");  // Prints: "number"
    document.write(typeof 2.5e-4 + "<br>");  // Prints: "number"
    document.write(typeof Infinity + "<br>");  // Prints: "number"
    document.write(typeof NaN + "<br>");  // Prints: "number". Despite being "Not-A-Number"
     
    // Strings
    document.write(typeof '' + "<br>");  // Prints: "string"
    document.write(typeof 'hello' + "<br>");  // Prints: "string"
    document.write(typeof '12' + "<br>");  // Prints: "string". Number within quotes is document.write(typeof string
     
    // Booleans
    document.write(typeof true + "<br>");  // Prints: "boolean"
    document.write(typeof false + "<br>");  // Prints: "boolean"
     
    // Undefined
    document.write(typeof undefined + "<br>");  // Prints: "undefined"
    document.write(typeof undeclaredVariable + "<br>"); // Prints: "undefined"
     
    // Null
    document.write(typeof Null + "<br>");  // Prints: "object"
     
    // Objects
    document.write(typeof {name: "John", age: 18} + "<br>");  // Prints: "object"
     
    // Arrays
    document.write(typeof [1, 2, 4] + "<br>");  // Prints: "object"
     
    // Functions
    document.write(typeof function(){});  // Prints: "function"
    </script>
</body>
</html>

Output :

number

number

number

number

number

string

string

string

boolean

boolean

undefined

undefined

undefined

object

object

function

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)

This topic belongs to these lists

need a help?


find thousands of online teachers now

New to examplegeek.com?

Join us