Quick Contact

    JavaScript Data Types

    Introduction to Data Types in JavaScript

    Data types define what forms of data may be saved and handled within a programme. Because JavaScript is a functional programming language, users do not need to define the type of the variable because it is utilized dynamically by the Javascript library. To indicate the type of data, users must use var here.

    In JavaScript, there are six fundamental data types that may be grouped into three categories: primitive (or primary), composite (or reference), and special data types. Primitive data types include String, Number, and Boolean. Object, Array, and Function (all object types) are composite data types.

    The String Data Type

    Textual data is represented by the strings database table (i.e. sequences of characters). Strings are formed by enclosing one or maybe more elements in single or double quotes, as shown below:

    Example

    < html>
    < body>
    < script>
    // Creating variables
    var a = "Hello sohan how are you.";
    var b = 'Sohancan not "Reply".';
    var c = 'We\'ll never give up.';
    
        // Printing variable values
    document.write(a + "< br>");
    document.write(b + "< br>");
    document.write(c);
    < /script>
    < /body>
    < /html>
    

    Output

    Hello sohan how are you.
    Sohancan not "Reply".
    We'll never give up.
    

    The Number Data Type

    The number data structure is utilized to describe positive and negative integers, either with or without a decimal point, as well as values expressed in exponential expression.

    Example

    < html>
    < body>
    < script>
    // Creating variables
    var p = 55;
    var q = 80.5;
    var r = 4.25e+6;
    var s = 4.25e-6;
    
        // Printing variable values
    document.write(p + "< br>");
    document.write(q + "< br>");
    document.write(r + "< br>");
    document.write(s);
    < /script>
    < /body>
    < /html>
    

    Output

    55
    80.5
    4250000
    0.00000425
    

    The Boolean Data Type

    The Boolean data type has only two possible values: true or false. It is commonly used to hold values like as yes (true) or no (false), on (true) or off (false), and so on, as seen below:

    Example

    < html>
    < body>
    < script>
    // Creating variables
    varisWorking = true;   
    varisNonworking = false; 
    
        // Printing variable values
    document.write(isWorking + "< br>");
    document.write(isNonworking);
    < /script>
    < /body>
    < /html>
    

    Output

    true
    false
    

    The Undefined Data Type

    The undefined data type can only contain one value: the undefined special price. If a variable has been declared but not allocated a value, its value is undefined.

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Undefined Data Type< /title>
    < /head>
    < body>
    < script>
        // Creating variables
    var a;
    var b = "Hello World!"
    
        // Printing variable values
    document.write(a + "< br>");
    document.write(b);
    < /script>
    < /body>
    < /html>
    

    Output

    undefined
    Hello World!
    

    The Null Data Type

    This is another unique data type that can only have single value: null. A null value indicates that no value exists. It’s not the same as an empty string (“”) or 0; it’s just nothing.

    By setting the null value to a variable, you may explicitly empty it of its present contents.

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Null Data Type< /title>
    < /head>
    < body>
    < script>
    var a = null;
    document.write(a + "< br>"); 
    
    var b = "Hello World!"
    document.write(b + "< br>"); 
    
        b = null;
    document.write(b) 
    < /script>
    < /body>
    < /html>
    

    Output

    null
    Hello World!
    null
    

    The Object Data Type

    The object is a sophisticated type which enables users to save data collections.

    An object has properties, which are specified as a primary key.

    A property key (name) always contains a sequence of bytes, but the value can include any data type, including strings, numbers, and booleans, as well as complex data types such as arrays, functions, and other objects.

    Objects will be covered in greater detail in subsequent chapters.

    The following example demonstrates the most basic technique to build an object in JavaScript.

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Object Data Type< /title>
    < /head>
    < body>
    < script>
    varemptyObject = {};
    var person = {"name": "Ajeet", "surname": "Maurya", "age": "33"};
    
        // For better reading
    var car = {
            "modal": "AUDI Q3",
            "color": "black",
            "doors": 4
        }
    
        // Print variables values in browser's console
    console.log(person);
    console.log(car);
    < /script>
    < p>< strong>Note:< /strong> Check out the browser console by pressing the f12 key on the keyboard.< /p>
    < /body>
    < /html>
    

    Output
    Note:

    By hitting the f12 key on the keyboard, you may access the browser console.

    If the name is a valid JavaScript name, you can remove the quotations surrounding it. That is, quotation marks are necessary around “first-name,” while they are optional surrounding firstname.

    The Array Data Type

    An array is a type of object that allows you to store several objects in a single variable. Every value (also called as an element) in an array has a numeric location known as its index and can include any data type-numbers, texts, booleans, functions, objects, and even other arrays. The array index begins at zero, therefore the first array element is arr[0], not arr[1].

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Array Data Type< /title>
    < /head>
    < body>
    < script>
        // Creating arrays
    var colors = ["Red", "Green", "Blue", "Yellow"];
    var cities = ["London", "Paris", "New York"];
    
        // Printing array values
    document.write(colors[0] + "< br>");       document.write(cities[2]);   
    < /script>
    < /body>
    < /html>
    

    Output

    Red
    New York
    

    The Function Data Type

    The function is a callable object which runs a section of code. Because functions are objects, they may be assigned to variables, as seen in the following example:

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Function Passed as Argument to Other Function< /title>
    < /head>
    < body>
    < script>
    functioncreateGreeting(name){
    return "Hello, " + name;
        }
    functiondisplayGreeting(greetingFunction, userName){
    returngreetingFunction(userName);
        }
    
    var result = displayGreeting(createGreeting, "Ajeet");
    document.write(result); 
    < /script>
    < /body>
    < /html>
    

    Output

    Hello, Ajeet

    The typeof Operator

    The typeof operator can be utilized to determine the type of information contained in a parameter or argument. It is OK to use it with or without parenthesis (typeof(x) or typeof x).

    The typeof operator is really handy when you need to treat values of different kinds differently, but be careful since it might generate unexpected results in some circumstances, as seen in the following example:

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript typeof Operator< /title>
    < /head>
    < body>
    < script>
        // Numbers
    document.write(typeof 11 + "< br>");  
    document.write(typeof 32.6 + "< br>");  
    document.write(typeof 2.5e-4 + "< br>");  
    document.write(typeof Infinity + "< br>");  
    document.write(typeofNaN + "< br>");  
    
        // Strings
    document.write(typeof '' + "< br>");  
    document.write(typeof 'hello' + "< br>");  
    document.write(typeof '12' + "< br>");  // 
    
        // Booleans
    document.write(typeof true + "< br>");  
    document.write(typeof false + "< br>");  
    
        // Undefined
    document.write(typeof undefined + "< br>");  
    document.write(typeofundeclaredVariable + "< br>"); 
    
        // Null
    document.write(typeof Null + "< br>");  
    
        // Objects
    document.write(typeof {name: "Ravi", age: 33} + "< br>");  
    
        // Arrays
    document.write(typeof [1, 2, 4] + "< br>");  
    
        // Functions
    document.write(typeof function(){});  
    < /script>
    < /body>
    < /html>
    

    Output

    Output
    number
    number
    number
    number
    number
    string
    string
    string
    boolean
    boolean
    undefined
    undefined
    undefined
    object
    object
    function
    

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.