JavaScript Functions
- The one-liner code that concatenates all strings passed into a function is
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. apply takes an array of arguments and treats each element of that array as a single argument.
- If you have a function f and an object o, you can define a method named m of o with
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
A method is nothing more than a JavaScript function that is stored in a property of an object. If you have a function fun and an object obj, you can define a method named meth of obj with the following line:
obj.meth = fun;
- For the below mentioned code snippet:
var obj = new Object();
The equivalent statement is:
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
As a special case, for the new operator only, JavaScript simplifies the grammar by allowing the parenthesis to be omitted if there are no arguments in the function call. Hence you can always omit a pair of empty parentheses in a constructor invocation.
- What is the difference between the two lines given below ?
!!(object1 && object2);
(object1 && object2);
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
The first returns a “real” boolean value, because you first negate what is inside the parenthesis, but then immediately negate it again. So, it’s like saying something is “not not” truth-y, making it true. The second example simply checks for the existence of the object1 and object2, but might not necessarily return a “real” boolean value, instead returning something that is either truth-y or false-y. This can be problematic, because false-y can be the number 0, or an empty string, etc. Simple existence can be truth-y. A “real” boolean will only be true or false.
- Consider the following code snippet :
var p = counter(), q = counter();
p.count()
q.count()
p.reset()
p.count()
q.count()
The state stored in q is :
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
Counter function increments the value of the variable by 1 and reset function sets the value of the variable back to 0.as q is incremented twice and reset function is not called on q therefore value of q is 2.