Skip to content

Coding-with-Tushar/Javascript-Output-Based-Question

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 

Repository files navigation

Javascript Output Based Question

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
      1. 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.

      2. 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.

      3. 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.

      4. 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.

      5. 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.

      6. 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.

Scroll to Top

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.

Scroll to Top

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.

Scroll to Top

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.

Scroll to Top

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.

Scroll to Top

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.

Scroll to Top

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.

Scroll to Top

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.

Scroll to Top

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.

Scroll to Top

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.

Scroll to Top

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.

Scroll to Top

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

Scroll to Top

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.

Scroll to Top

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.

Scroll to Top

About

Understand the Javascript through important output based questions.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors