What are the different data types in JavaScript?
JavaScript has seven primitive data types: string, number, bigint, boolean, undefined, symbol, and null. It also includes one non-primitive data type: object. Objects include arrays, functions, and regular expressions. Each of these types serves different purposes in programming.
How do you check the data type of a variable in JavaScript?
You can check the data type of a variable in JavaScript using the `typeof` operator. For example, `typeof variableName` returns a string indicating the variable's type, such as "number", "string", "boolean", "object", "function", or "undefined".
What are the differences between primitive and reference data types in JavaScript?
Primitive data types are immutable and stored directly in the stack, including types like string, number, boolean, null, undefined, and symbol. Reference data types, such as objects, arrays, and functions, are mutable and stored as references in the heap, with variables pointing to their memory address.
How do you convert between different data types in JavaScript?
In JavaScript, you can convert data types using functions like `String()`, `Number()`, and `Boolean()`. You can also use methods like `.toString()` for string conversion or use `parseInt()` and `parseFloat()` for converting strings to numbers. Implicit conversion occurs with operators like `+` and `==`. Use `JSON.stringify()` and `JSON.parse()` for objects.
What is the purpose of each data type in JavaScript?
In JavaScript, data types serve distinct purposes: "Number" for numerical values, "String" for text, "Boolean" for true/false values, "Undefined" for unassigned variables, "Null" for intentional absence of value, "Object" for collections of key-value pairs, and "Symbol" for unique identifiers; "BigInt" is used for large integers.