1.
Var a = "";
var b = ['foo', 'max', 'mike'];
Make a = "foomaxmike" in a single line of code.
2.
If you have seen all the data types in Javascript, then you have seen a new data type in JavaScript called Symbol. Can you tell me what is the use of it?
3.
How to delete cookies in JavaScript?
4.
Create a function that, given a DOM Element on the page, will visit the element itself and all of its descendents (not just its immediate children). For each element visited, the function should pass that element to a provided callback function. The arguments to the function should be: 1.) a DOM element 2.) a callback function (that takes a DOM element as its argument)
5.
Detect the console value of the following line of code. function Foo(a,b,c){
this.x = a;
this.y = b;
this.z = c;
}
var m = Foo(3,4,5);
var k = {};
Foo.call(k,3,4,5);
console.log(k);
console.log(m);
Correct Answer
D. {3,4,5}, undefined
Explanation
The code snippet defines a constructor function `Foo` that takes three arguments `a`, `b`, and `c`. Inside the function, it assigns the values of `a`, `b`, and `c` to the properties `x`, `y`, and `z` of the newly created object using the `this` keyword.
In the first line after the function definition, `var m = Foo(3,4,5);`, the `Foo` function is called without using the `new` keyword. This means that it is not used as a constructor and does not create a new object. Instead, it simply executes the code inside the function and returns `undefined`. Therefore, `m` is assigned the value `undefined`.
In the next line, `var k = {};`, a new empty object `k` is created.
Then, `Foo.call(k,3,4,5);` is called, which executes the `Foo` function with `k` as the `this` value and passes `3`, `4`, and `5` as arguments. This assigns the values `3`, `4`, and `5` to the properties `x`, `y`, and `z` of `k`.
Finally, `console.log(k);` logs the value of `k`, which is `{3,4,5}`, and `console.log(m);` logs the value of `m`, which is `undefined`.
6.
How many data types are there in javascript?
Correct Answer
B. 6
Explanation
There are six data types in JavaScript. These include number, string, boolean, object, null, and undefined. Each data type is used to store different types of values and has its own set of properties and methods. The number data type is used to store numeric values, the string data type is used to store textual values, the boolean data type is used to store true or false values, the object data type is used to store complex data structures, the null data type is used to represent the absence of any object value, and the undefined data type is used to represent variables that have been declared but have not been assigned a value.
7.
Var k = {}
k[Symbol('lorem')] = "Max";
k[Symbol('lorem')] = "Payne";
console.log(k[Symbol('lorem')]);console value is?
Correct Answer
C. Undefned
Explanation
The code creates an empty object `k` and assigns two properties to it using Symbol keys. The first property is assigned the value "Max" and the second property is assigned the value "Payne". When trying to access the value of `k[Symbol('lorem')]`, it will return `undefined` because the Symbol keys are unique and cannot be accessed using any other means except the exact same Symbol key used for assignment. Therefore, the correct answer is "undefined".
8.
Is "" == 0?
Correct Answer
A. True
Explanation
The expression "" == 0 is comparing an empty string with the number 0. In JavaScript, when comparing values of different types, the interpreter will try to convert one or both of the values to a common type before making the comparison. In this case, the empty string is converted to a number, resulting in 0. Therefore, the expression "" == 0 is true because both sides of the comparison are equal.
9.
(function(){
var a = b = 3;
})();
console.log(typeof a !== 'undefined');
console.log(typeof b !== 'undefined'); what are console values of above?
Correct Answer
C. False, true
Explanation
In the given code, there is a self-invoking function that declares a variable "a" and assigns it the value of 3. However, the variable "b" is not declared with the "var" keyword, so it becomes a global variable. Therefore, "a" is not accessible outside the function and its type is undefined. On the other hand, "b" is accessible globally and its type is not undefined. Hence, the console values are false for the first statement and true for the second statement.
10.
What is the output of console.log(0.1 + 0.2 == 0.3)?
Correct Answer
B. False
Explanation
The output of the expression `console.log(0.1 + 0.2 == 0.3)` is `False`. This is because in JavaScript, due to floating point precision, the addition of 0.1 and 0.2 does not result in an exact value of 0.3. Therefore, the comparison `0.1 + 0.2 == 0.3` evaluates to `False`.
11.
What is console.log(1 && 2);
Correct Answer
C. 2
Explanation
The expression "1 && 2" is evaluated using the logical AND operator. In JavaScript, the logical AND operator returns the value of the second operand if the first operand is truthy (not equal to false, 0, "", null, undefined, or NaN), otherwise, it returns the value of the first operand. In this case, both 1 and 2 are truthy values, so the expression evaluates to 2.