What will be the equivalent code of the following JavaScript code?
for(var p in o)
console.log(o[p]);
a) for (var i = 0;i < a.length;i++)
console.log(a[i]);
b) for (int i = 0;i < a.length;i++)
console.log(a[i]);
c) for (var i = 0;i <= a.length;i++)
console.log(a[i]);
d) for (var i = 1;i < a.length;i++)
console.log(a[i]);
Answer: a
Explanation: The in variable does the same task of traversing the array starting from the 0 index. The for/in loop makes it easy to do the same that we do using a for.
Related Posts
What would be the result or type of error if p is not defined in the following JavaScript code snippet?
console.log(p)What will be the output of the following JavaScript code if oddsums(5); is executed after the following code snippet?
function oddsums(n)
{
let total = 0, result=[];
for(let x = 1; x <= n; x++)
{
let odd = 2*x-1;
total += odd;
result.push(total);
}
return result;
}The main difference between the variables declared with var and with let is __________
The “let” keyword cannot be used ___________
What will be the output of the following JavaScript code?
const pi=3.14;
var pi=4;
console.log(pi);What will be the output of the following JavaScript code?
set.add(“1”);
set.add(“2”);
document.writeln(set.has(“3”));What will be the output of the following JavaScript code?
set.add(“one”);
set.add(“two”);
var itr=set.values();
document.writeln(itr.next().value);
Join The Discussion