JavaScript is known for its flexibility, which is partly due to its dynamic typing system. Unlike some other languages, JavaScript does not require you to declare the data type of a variable before using it. Instead, the type of a variable is determined at runtime based on the value assigned to it. This means that a single variable can hold different types of values at different times during the execution of a program. In this article, we’ll explore the different types in JavaScript and how they work.
- Primitive Types
JavaScript has six primitive data types: string, number, boolean, null, undefined, and symbol.
- String: Represents a sequence of characters, enclosed in either single or double quotes. For example, “hello world” or ‘123’.
- Number: Represents a numeric value, including integers, floats, and NaN (not a number). For example, 42 or 3.14.
- Boolean: Represents a logical value, either true or false.
- Null: Represents the intentional absence of any object value. It is a primitive value and not an object.
- Undefined: Represents a variable that has been declared but not assigned a value.
- Symbol: A new data type introduced in ECMAScript 6, used to create unique identifiers.
- Object Types (Non-Primitive Type)
In addition to the primitive types, JavaScript also has object types. Objects are collections of key-value pairs, where the key is a string or symbol and the value can be any data type, including other objects.
- Object: The most common object type in JavaScript, used to store collections of properties and methods.
- Array: A type of object that is used to store a collection of values, typically of the same data type.
- Function: A type of object that can be invoked with arguments to perform a specific task.
In javascript we have typeof to check what is the type of a variable.
Example Questions on type
typeof 5 //number
typeof true //booleanÂ
type of 'to be..' //string
typeof undefined //undefined
typeof null // object
typeof Symbol('just me') // symbol
typeof {} // object
typeof [ ] //object
typeof function(){} //function.
- Type Coercion
JavaScript is known for its flexible type system, which allows for implicit type conversion (or coercion) between different data types. This can be useful in some cases, but it can also lead to unexpected behavior if you’re not careful. For example:
- “5” + 2 = “52” (the number 2 is coerced into a string and concatenated with “5”)
- “5” – 2 = 3 (the string “5” is coerced into a number and subtracted from 2)
To avoid unexpected behavior, it’s important to understand how type coercion works and use explicit type conversion when necessary.
some common type coercion questions:
0 == []
0 == {}
0 == null
false == ""
false == []
false == {}
"" == 0
"" == []
"" == {}
4. Pass by Value and Pass by Reference
When passing an argument by value, a copy of the argument is created and passed to the function. This means that any changes made to the argument inside the function do not affect the original value.
When passing an argument by reference, a reference to the original value is passed to the function. This means that any changes made to the argument inside the function will also affect the original value.
Questions generally asked from this part are as below
Q:Guess the output
var P = ['a','b','c'];
var T = P;
T.push('d');
console.log(P);
console.log(T);
Hint: object is passed by reference in javascript
Q:Guess the output
var obj1 = {name : "coding", url: "codingcloud"};
var obj2 = {name : "coding", url: "codingcloud"};
var eq = obj1 == obj2;
alert(eq);
In the above question output would be false because we are comparing obj1 and obj2 memory address not their content. To compare their content without writing the deep clone method we can use the below method.
JSON.stringify(obj1) === JSON.stringify(obj2)
JSON.stringify will change obj1 and obj2 in string format which you can easily match using the === operator.
Understanding the different types in JavaScript and how they work is essential for any developer working with this language. From primitive types to objects, understanding the nuances of each data type is crucial for writing efficient and effective code.