Data Types
MERN Practical Explanation
Posted on February 7, 2024
JavaScript Data Types
JavaScript provides different data types to hold different types of Values.
There are two types of data types in Java Script:
- Primitive data type: it means ,it represent single value
- Non- primitive data type: an object which represents a collection of values.
Var a = 40://holding number
var b="Rahul"; // holding String
JavaScript Primitive data types
String: represents Sequence of characters e.g. "hello"
Number: represents numeric Values e.g. 100
Boolean Represents boolean Value either false or true
Underfined: represents undefined Value
Null: represents null i.e. no value at all
1:)Primitive: this value are immutable. this are pass by value.means To store single values.e.g.String,Number,Boolean,Undefined,Null.
Primitive types: Primitive data types can store only a single value.
typeof of primitive types :
typeof "John Doe" // Returns "string"
typeof 3.14 // Returns "number"
typeof true // Returns "boolean"
typeof 234567890123456789012345678901234567890n // Returns bigint
typeof undefined // Returns "undefined"
typeof null // Returns "object" (kind of a bug in JavaScript)
typeof Symbol('symbol') // Returns Symbol
In JavaScript, when we pass a primitive data type (such as Number, String, Boolean, Undefined, Null, or Symbol) to a function, it is passed by value. This means that a copy of the value is created and passed to the function, so that the original value remains unchanged.
For example:
let x = 10;
function changeValue(num) {
num = 20;
}
changeValue(x);
console.log(x); // Output: 10
In this example, the changeValue function takes a num parameter, which is a primitive data type (Number). When we call changeValue(x), a copy of the value of x (10) is passed to the function. Within the function, we assign a new value of 20 to num. However, this change does not affect the original value of x, which remains 10.
This is because primitive data types are passed by value in JavaScript, meaning that a copy of the value is created and passed to the function, so that the original value remains unchanged
2:) Non Primitive
(To store multiple values)->( value are mutable) (pass by reference):Object,Array.
Non-primitive types
To store multiple and complex values, non-primitive data types are used.
In JavaScript, when we pass an object (including arrays and functions) to a function, it is passed by reference. This means that a reference to the object, is passed to the function.
For example:
let person = { name: "John Doe", age: 26 };
function changeName(obj) {
obj.name = "Jane Doe";
}
changeName(person);
console.log(person.name); // Output: "Jane Doe"
In this example, the changeName function takes an obj parameter, which is an object. When we call changeName(person), a reference to the person object is passed to the function. Within the function, we change the name property of the object to "Jane Doe".
Since objects are passed by reference in JavaScript, this change is reflected in the original person object, so that its name property is now "Jane Doe".
This is because objects in JavaScript are passed by reference, meaning that a reference to the object, rather than a copy of its value, is passed to the function. Any changes made to the object within the function are therefore reflected in the original object, as it is the same object.
11) The keyword is a reference variable that refers to the current object.For example:
13)The parseInt() function is used to convert numbers between different bases.
parseInt() takes the string to be converted as its first parameter.
The second parameter is the base of the given string.
Mutable is a type of variable that can be changed. In JavaScript, only objects and arrays are mutable, not primitive values.
Why we use await ?
Ans-Await is used for calling an async function and waits for it to resolve or reject .
await blocks the execution of the code within the async function in which it is located. Async/Await makes it easier to write promises.
The keyword 'async' before a function makes the function return a promise, always. And the keyword await is used inside async functions, which makes the program wait until the Promise resolves.What is mutable?
Ans-A mutable object is an object whose state can be modified after it is created.
Immutables are the objects whose state cannot be changed once it is created. Strings and Numbers are Immutable.
let x= {}, y = {name:"Ronny"},z = {name:"John"};
x[y] = {name:"Vivek"};
x[z] = {name:"Akki"};
console.log(x[y]);
In JavaScript, when an object is used as a key in another object, it is automatically converted to a string using the toString() method. In this case, both y and z are objects, so they will be converted to strings and used as keys in the x object.
Here's what's happening in the code:
let x = {}, y = {name:"Ronny"}, z = {name:"John"};
x[y] = {name:"Vivek"}; // Set x["[object Object]"] = {name:"Vivek"}
x[z] = {name:"Akki"}; // Set x["[object Object]"] = {name:"Akki"}
console.log(x[y]); // Logs {name:"Akki"}
In this code, the x object is being assigned two properties: x[y] and x[z]. Since y and z are both objects, they are both converted to the string "[object Object]" and used as keys in the x object.
The first assignment sets the x["[object Object]"] property to {name:"Vivek"}. The second assignment overwrites the x["[object Object]"] property with {name:"Akki"}.
When console.log(x[y]) is called, it retrieves the value of x["[object Object]"], which is {name:"Akki"} because it was the last value assigned to that key.
Pundarikaksha Mishra6:04 PM
let a = 0;
let b = false;
console.log((a == b));
console.log((a === b));
what is concurrent in node js
Concurrency is an essential feature of Node. js that enables it to handle large numbers of I/O operations simultaneously, without blocking the execution thread. This concurrency model allows Node. js to handle many requests concurrently, resulting in improved application performance
JavaScript Operators
JavaScript Operators are symbols that are used to perform Operations on operands.
JavaScript includes following categories of operators.
Arithmetic Operators: use to perform arithmetic operation on the operands.
E. g:
Operator Description Example
- Addition 10+20 = 30
- Subtraction 20-10 = 10
- Multiplication 10*20 = 200 / Division 20/10 = 2 % Modulus (Remainder) 20%10 = 0 ++ Increment var a=10; a++; Now a = 11 -- Decrement var a=10; a--; Now a = 9
Comparison Operators:
The JavaScript comparison operator compares the two operands. The comparison operators are as follows:
Operator Description Example
== Is equal to 10==20 = false
=== Identical (equal and of same type) 10==20 = false
!= Not equal to 10!=20 = true
!== Not Identical 20!==20 = false
Greater than 20>10 = true
= Greater than or equal to 20>=10 = true
< Less than 20<10 = false
<= Less than or equal to 20<=10 = false
Logical Operators:
The following operators are known as JavaScript logical operators.
Operator Description Example
&& Logical AND (10==20 && 20==33) = false
|| Logical OR (10==20 || 20==33) = false
! Logical Not !(10==20) = true
Assignment Operators:
The following operators are known as JavaScript assignment operators.
Operator Description Example
= Assign 10+10 = 20
+= Add and assign var a=10; a+=20; Now a = 30
-= Subtract and assign var a=20; a-=10; Now a = 10
= Multiply and assign var a=10; a=20; Now a = 200
/= Divide and assign var a=10; a/=2; Now a = 5
%= Modulus and assign var a=10; a%=2; Now a = 0
Java Script Special Operators
The following Operators are known as JavaScript Special operators.
(?:) : Conditional Operator returns Value based on
the Condition. It is like if- else. Comma Operator allows multiple expressions to be evaluated as single statement.
Delete: Delete Operator deletes a property from the object.
In: In Operator checks if object has the given property.
Instanceof: Checks if the object is an instance of given type
New: Creates an instance
Typeof: Checks the type of object .
Void : it discards the expression's return value.
Yield: checks what is returned in a generator by the generator's interator.
Posted on February 7, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.