Every contribution counts, regardless of its size. I value and appreciate the efforts of all contributors, from beginners to seasoned developers. Join me on this exciting journey of open-source collaboration. Together, let's build something amazing! 🤝
- 👉 Please ensure that your contributions adhere to the coding style and guidelines of this project
- 👉 Include clear and concise commit messages for all your commits
- 👉 Provide a detailed description of your changes in the pull request.
- 👉 Be respectful and considerate towards other contributors.
Type Conversion
Boolean | String | Number | |
---|---|---|---|
"" | false | "" | 0 |
" " | true | " " | 0 |
"5" | true | "5" | 5 |
"5hello" | true | "5hello" | NaN |
Boolean | String | Number | |
---|---|---|---|
0 | false | "0" | 0 |
-0 | false | "-0" | -0 |
1 | true | "1" | 1 |
-1 | true | "-1" | -1 |
Infinity | true | "Infinity" | Infinity |
-Infinity | true | "-Infinity" | -Infinity |
NaN | false | "NaN" | NaN |
Boolean | String | Number | |
---|---|---|---|
null | false | "null" | 0 |
undefined | false | "undefined" | NaN |
Boolean | String | Number | |
---|---|---|---|
[] | true | "" | 0 |
{} | true | "[object Object]" | NaN |
["1"] | true | "1" | 1 |
["1","2"] | true | "1,2" | NaN |
[1,"2",[3,4],5,6,[],7,8,{},{age:21}] | true | "1,2,3,4,5,6,,7,8,[object Object],[object Object]" | NaN |
Notes for Javascript Operator
- if undefined tries to convert itself into number then it will converted into NaN(Not a number).
- if any of operand is object or array then they will be converted into primitive(number or string)
- arithmetic + operator
- if any of the operand is string then the + operator will concatenate both the operands.
- else the addition will be done and for this the operand will be first converted into numbers and then addition will take place.
- arithmetic - operator
- In JavaScript, when the subtraction operator - is used, the operands are converted to numbers before performing the subtraction
- Comparison > operator or Comparions < operator
- For > operator Both the operands will be converted into numbers in this case and then comparison of greater than or less than will take plac
- Comparison === operator
- It does not do any type conversion and only give true if both the data type and value of operands is same.
- And there is no special case for null and undefined but it was there in == operator.
- /null === null(true) undefined === undefined(true) null === undefined(false)
- Comparison == operator
-
If the types are the same:If both operands are of the same type, == simply checks for equality without any type conversion. Example: "5" == "5" is true, and 5 == 5 is true.
-
If one operand is null and the other is undefined: JavaScript treats null and undefined as equal. So null == undefined is true, but they are not coerced to any other type. Example: null == undefined → true.
-
If one operand is a number and the other is a string: JavaScript converts the string to a number and then performs the comparison. Example: 5 == "5" → true because "5" is converted to 5.
-
If one operand is a boolean: JavaScript converts the boolean to a number (true to 1 and false to 0) and then performs the comparison. Example: true == 1 → true because true is converted to 1.
-
If one operand is an object and the other is a primitive (number, string, etc.): JavaScript attempts to convert the object to a primitive using the object’s .valueOf() or .toString() methods. After converting the object to a primitive, JavaScript applies the comparison. Example1: [1]=="1" ->true because [1] is converted to "1" . Example2: [1] == 1 → true because [1] is converted to "1", which then converts to 1.
-
Special Case for NaN: NaN is never equal to anything, including itself. Example: NaN == NaN → false.
Statement vs Expression
- Expression:-We can store the result of Expression in a variable.example:-Ternary operator,Function Expression,Function Calling,Operator based expressions ,Array forEach Loop.
- Statement:-We can not store the result of Statement in a variable and if we try to do this we will get Error.examples:-IfElse statement,for loop,Switch,Vairable Declaration.
Weird things of JavaScript
- The Boolean converstion of string which has only spaces is true and type conversion of empty string is false.But if these both strings are converted into numbers then they both are converted to 0. For the empty string it is obvious but for the string with spaces it is a weird thing that why did it gets converted to 0
- Boolean of [] is true but number conversion of [] is 0.It is because first the [] will be converted into string "" and the number conversion of empty string and string with only spaces is 0.
- If we do comparison of NaN with anyone then we will get false everytime.(EVEN IF WE DO COMPARISON OF NAN WITH NAN , WE WILL GET FALSE )
1. What will be the output
let a = undefined;
let b = 10;
let c = a + b;
console.log(c);
View Answer
- Output : NaN
- Reason : Here as none of the operand is string so the + will do addition here and for this addition to take place it will make both the operands of same type i.e number type.Here,undefined will be converted into NaN and any computation with NaN yields in NaN.
2. What will be the output
console.log(NaN == NaN);
console.log(NaN === NaN);
console.log(NaN !== NaN);
View Answer
- Output : false,false,false
- Reason : Any comparison of NaN results in false.
3. What will be the output
let a = { name: "Alice" };
let b = [1, 2, 3];
let c = a + b;
console.log(c);
View Answer
- Output : [object Object]1,2,3
- Reason : The object gets converted to "[object Object]", and the array gets converted to "1,2,3".And after type conversion as anyone of the operand is in string format then concatenation will take place.
4. What will be the output
let a = 5;
let b = {};
let c = a - b;
console.log(c);
View Answer
- Output : NaN
- Reason : JavaScript tries .toString() on {}, which results in the string "[object Object]".and When JavaScript attempts to convert "[object Object]" to a number, it fails and results in NaN (Not a Number).And finally Any arithmetic operation involving NaN results in NaN.
5. What will be the output
console.log(null == undefined);
console.log(null === undefined);
console.log(null == null);
View Answer
- Output : true,false,true
- Reason : null == undefined is true because they are considered equal. null === undefined is false because they are of different types. null == null is true.
6. What will be the output
let a = 2;
let b = "2";
let c = a + b - a;
console.log(c);
View Answer
- Output : 20
- Reason : a + b becomes 2 + "2", resulting in "22" (string concatenation). Then, "22" - a becomes "22" - 2, which coerces the string back to a number, yielding 20.
7. What will be the output
console.log(typeof (5 + "5"));
console.log(typeof (5 - "5"));
View Answer
- Output : string,number
- Reason :5 + "5" results in the string "55", so typeof is string. 5 - "5" coerces the string to a number, resulting in 0, so typeof is number.
8. What will be the output
const result=if(5>4)return "true"
else return "false"
View Answer
- Output : Syntax Error
- Reason If else is a statement and we cannot store the result of it in a variable and if we try to store its result we will get syntax error.
9. What will be the output
let x=let y=5;
View Answer
- Output : Syntax Error
- Reason Variable Declaration is a statement and we cannot store the result of it in a variable and if we try to store its result we will get syntax error.
10. What will be the output
let result=5>4?"Hello World":"Hello India";
console.log(result)
View Answer
- Output : Hello World
- Reason Ternary operator is an expression and thus we can store the result of it in a variable and we wil get Hello World here as output.
11. What will be the output
const user={
firstName:"John"
}
console.log(user.firstName)
console.log(user["firstName"])
console.log(user[firstName])
View Answer
- Output : John,John,Reference Error
- Reason user[firstName] expression will look into the global scope for variable firstName and will not able to find any variable with name firstName and thus gives us error.
12. What will be the output
let firstName="myName"
const user={
myName:"John"
}
console.log(user.myName)
console.log(user["myName"])
console.log(user[firstName])
View Answer
- Output : John,John,John
- Reason user[firstName] expression will look into the global scope for variable firstName and will be succedd in finding variable with name firstName and thus replace the firstName variable with "myName" and then look into the user object for the key myName and then gives us its value.
13. What will be the output
function multiply(a,b=1){
console.log(a*b)
}
multiply(5,4);
multiply(5);
multiply(5,null);
multiply(5,"");
multiply(5," ");
multiply(5,"hello");
multiply(5,false);
multiply(5,undefined);
View Answer
- Output : 20,5,0,0,0,NaN,0,5
- Reason The default value will only be taken if we pass undefined or if we didn't pass the argument.In other cases multiplication will happen after type conversion
14. What will be the output
const person = {
firstName: 'Tushar',
};
const { lastName="Chawla" } = person;
console.log(lastName);
View Answer
- Output : Chawla
- Reason The lastName property is not defined in the person object and the destructuring syntax provides a default value ("Chawla") to be used when the property is missing.
15. What will be the output
const person = {
firstName: 'Tushar',
lastName:undefined,
};
const { firstName="John",lastName="Chawla" } = person;
console.log(firstName,lastName);
View Answer
- Output : Tushar,Chawla
- Reason The `firstName` property in the `person` object has the value 'Tushar'. The default value "John" is ignored because it only applies when the property does not exist or is `undefined` and same reason for the lastName property.
16. What will be the output
function changeAgeAndReference(person) {
person.age = 25;
person = {
name: 'John',
age: 50
};
return person;
}
const personObj1 = {
name: 'Alex',
age: 30
};
const personObj2 = changeAgeAndReference(personObj1);
console.log(personObj1); // -> ?
console.log(personObj2); // -> ?
View Answer
- Output : { name: 'Alex', age: 25 } { name: 'John', age: 50 }
- Reason The function first changes the property age on the original object it was passed in. It then reassigns the variable to a brand new object and returns that object. Here’s what the two objects are logged out.
17. What will be the output
let person = { name: 'Lydia' };
const members = [person];
person=null
console.log(members);
View Answer
- Output : [{ name: 'Lydia' }]
- Reason When you assign an object (e.g., person) to another variable or include it in an array, only the reference to the object is stored, not the object itself.In this case, members[0] holds a reference to the person object. This does not affect the actual object that person originally referenced. It only reassigns the person variable to null, breaking the reference between person and the object. The object { name: 'Lydia' } itself still exists in memory as long as something else (like the members array) holds a reference to it.
18. What will be the output
function func1(){
console.log("global func1")
}
const obj={
func1(){
console.log("local func1")
},
func2(){
console.log("local func3")
},
func3(){
obj.func1();
func1();
func2();
},
}
obj.func3();
View Answer
- Output : local func1,global func1,ReferenceError: func2 is not defined
- ReasonWe need to keep one thing in our mind that the properites of object can be access either using dot notation or bracket notation.So,in this code the 1st console statement result is understandable.Now after that we try to call func1() method the js will look for this func1 method inside the func3 scope and here it will not able to find it ,so it will check func1 in global scope directly and will call the global func1 function.Now for the func2() call , the js will neither able to find fuc2 in func3 scope nor in global scope so it will give error that func2 is not defined.
19. What will be the output
function func1(){
console.log("global func1",this)
}
const obj={
func1(){
console.log("local func1",this)
},
func2(){
console.log("local func3",this)
},
func3(){
obj.func1();
func1();
func2();
},
}
obj.func3();
View Answer
- Output : local func1 {func1: ƒ, func2: ƒ, func3: ƒ} ,global func1 Window {window: Window, self: Window, document: document, name: '', location: Location, …},ReferenceError: func2 is not defined
- ReasonSimilar to above question.
20. What will be the output
const obj={
firstName:this,
pata:{
pinCode:this,
moreDetails:function(){
console.log(this)
}
}
}
console.log(obj.firstName)
console.log(obj.pata.pinCode)
console.log(obj.pata.moreDetails())
View Answer
- Output : Window {window: Window, self: Window, document: document, name: '', location: Location, …},Window {window: Window, self: Window, document: document, name: '', location: Location, …},{pinCode: Window, moreDetails: ƒ}
- Reason Reason to be published.
21. What will be the output
function demo(){
const obj={
firstName:this,
address:{
pata:this
},
findBlood(){
console.log(this)
return ""
}
}
console.log(obj.firstName)
console.log(obj.address.pata)
console.log(obj.findBlood())
}
demo()
View Answer
- Output : Window {window: Window, self: Window, document: document, name: '', location: Location, …},Window {window: Window, self: Window, document: document, name: '', location: Location, …},{firstName: Window, address: {…}, findBlood: ƒ}
- Reason Reason to be published.