JavaScript - Datatypes & HoistingData Types :JavaScript is a dynamically typed scripting language. In JavaScript, variables can receive different data types over time. The latest ECMAScript standard defines eight data types Out of which seven data types are Primitive (predefined) and one complex or Non-Primitive.i. Primitive Data Types : The predefined data types provided by JavaScript language are known as primitive data types. Primitive data types are also known as in-built data types.
The data types that are derived from primitive data types of the JavaScript language are known as non-primitive data types. It is also known as derived data types or reference data types. // Numbers: let length = 16; let weight = 7.5; // Strings: let color = "Yellow"; let lastName = "Johnson"; // Booleans let x = true; let y = false; // Object: const person = {firstName:"John", lastName:"Doe"}; // Array object: const cars = ["Mahindra", "Tata", "BMW"]; // Date object: const date = new Date("2024-08-31");Main example for Primitive and Non-primitive datatypes: < !DOCTYPE html> < html> < body> < h2>JavaScript Objects< /h2> < p id="demo">< /p> < hr> < h1>JavaScript Operators< /h1> < h2>The typeof Operator< /h2> < p>The typeof operator returns the type of a variable or an expression.< /p> < script> const person = { firstName : "Java", lastName : "Script", age : 39, eyeColor : "blue" }; document.getElementById("demo").innerHTML = person.firstName + " is " + person.age + " years old."; document.getElementById("demo1").innerHTML = typeof 0 + "< br>" + typeof 314 + "< br>" + typeof 3.14 + "< br>" + typeof (3) + "< br>" + typeof (3 + 4); < /script> < /body> < /html>Output : JavaScript Objects Java is 39 years old. ________________________________________ JavaScript Operators The typeof Operator The typeof operator returns the type of a variable or an expression. Hoisting :Javascript allows a programmer to a member(variable) before the declaration state this characteristics is known as hoisting. JavaScript Declarations are Hoisted.In JavaScript, a variable can be declared after it has been used. In other words, a variable can be used before it has been declared.Example 1 gives the same result as Example 2: Example 1: x = 5; // Assign 5 to x elem = document.getElementById("demo"); // Find an element elem.innerHTML = x; // Display x in the element var x; // Declare x Example 2: var x; // Declare x x = 5; // Assign 5 to x elem = document.getElementById("demo"); // Find an element elem.innerHTML = x; // Display x in the element Eg: console.log(x); //undefined var x= “Javascript”; console.log(x); //Javascript « Previous Next Topic » (JS - Operators) |