Data types define the type of value/data stored in a variable, value may be stored directly or entered by the user. We can put any data type value in a variable. JavaScript is "dynamically typed" language, means a variable can be a string at one moment and then a number:
let message = "hello"; message = 123; // no error
Programming languages that allow such things are called "dynamically typed"
JavaScript data types are:
The number type represents both integer and floating-point numbers.
let n = 123; //integer value n = 12.345; //floating-point value
Infinity, -Infinity and NaN are "special numeric values"
console.log(1/0); // Infinity console.log("a"/2); // NaN, such division is error
In JavaScript, the "number" type cannot safely represent integer values larger than (253-1) (i.e. 9007199254740991), or less than -(253-1) for negatives, to store larger value use BigInt data type.
A BigInt value is created by appending n to the end of an integer value. eg:
const bigInt = 1234567890123456789012345678901234567890n;
A string in JavaScript is a text (small letters a-z, capital letter A-Z, special symbols-@, #, $) or number surrounded by quotes. In JavaScript, there are 3 types of quotes: Single quotes '...', Double quotes "...", and Backticks `...`.
let str1 = 'Hello World'; let str2 = "Single quotes are ok too"; let str3 = `can embed another variable ${str1}`; console.log(`the result is ${1+2}`); // the result is 3, embed an expression
The boolean data type has only two values: true and false.
let nameFieldChecked = true; // yes, name field is checked let ageFieldChecked = false; // no, age field is not checked let isGreater = 4 > 1; // true (the comparison result is "yes")
The "null" value: It's just a special value which represents "nothing", "empty" or "value unknown".
The "undefined" value: The meaning of undefined is "value not assigned". If a variable is declared, but not assigned, then its value is undefined:
let age; console.log(age); // shows "undefined"
To explicitly assign undefined to a variable:
let age = 100; age = undefined; // change the value to undefined console.log(age); // "undefined"
But it is not recommend doing that. Normally, null is used to assign an "empty" or "unknown" value to a variable, while undefined is reserved as a default initial value for unassigned.
All other types are called "primitive" data type because their values can contain only a single thing (be it a string or a number or whatever). but, objects are the collection which is used to store different types of data and more complex entities, that is why the object type is special.
The symbol type is used to create unique identifiers for objects.
The typeof operator returns the type of the value or operand. It's useful when we want to process values of different types. Eg:
console.log(typeof undefined); // "undefined" console.log(typeof 10); // "number" console.log(typeof 10n); // "bigint" console.log(typeof true); // "boolean" console.log(typeof "ankitweblogic"); // "string" console.log(typeof "100"); // "string" console.log(typeof Symbol("id")); // "symbol" console.log(typeof Math); // "object" (built-in object) console.log(typeof null); // "object" console.log(typeof alert); // "function"
typeof is used with or without brackets. Syntax: typeof(x) is same as typeof x. typeof is an operator, not a function.
What is the output of the script?
let website = "AnkitWebLogic"; Ans: console.log(`Welcome ${1}`); // ? Welcome 1 console.log(`Welcome ${"website"}`); // ? Welcome website console.log(`Welcome ${website}`); // ? Welcome AnkitWebLogic
Ad: