0

Explain the if condition here pls???And see the commented question in code

If([]==new Array() || {}==new Obj() || function(){}=new Function)/*...........why we don't give braces after new Function like new Obj()............*./ {console.log("true");} else{console.log("false");} //.....Ans: false.......

3rd Jan 2023, 11:25 AM
Mohammad Tausiful Alam
Mohammad Tausiful Alam - avatar
3 Réponses
+ 1
This code compares three expressions using the || operator, which means "or". The if statement will execute the block of code following it if any of the expressions on either side of the || operator evaluate to true. The first expression, [] == new Array(), compares two arrays using the equality operator (==). Because these arrays are different objects in memory, they are not equal, so this expression evaluates to false. The second expression, {} == new Obj(), compares two objects using the equality operator. Again, because these objects are different objects in memory, they are not equal, so this expression evaluates to false. The third expression, function(){} = new Function, is not a valid comparison. The = operator is used for assignment, not comparison. It appears that the intention was to compare these two objects using the equality operator (==), but this would also be invalid because the equality operator cannot be used to compare objects in JavaScript.
9th Jan 2023, 9:20 AM
Jash Gro
Jash Gro - avatar
+ 1
Cont: Since none of the expressions evaluate to true, the code in the else block will be executed, which will log "false" to the console. As for the question about the braces after new Function, in JavaScript, you can create a new object using the new operator followed by the constructor function for the object you want to create. The syntax for creating a new object using the Function constructor is new Function(arg1, arg2, ..., argN), where arg1, arg2, ..., argN are strings containing the argument names and function body for the new function. The braces after new Function are not necessary because the function body is defined in the arguments to the Function constructor.
9th Jan 2023, 9:20 AM
Jash Gro
Jash Gro - avatar
0
Jash Gro, But without any class how can i know this is a constructor function?
11th Jan 2023, 12:08 PM
Mohammad Tausiful Alam
Mohammad Tausiful Alam - avatar