Outputs of the following code

, ,

//Problem 1

console.log(a);
var a=12;

//Problem 2
console.log(Sum(2,3))
function Sum(a,b){
return a+b;
}

//Problem 3

console.log(0/0)

//Problem 4

const arr=[1,2,3,4,5,6,7,8]
console.log(arr.slice(1,-1))

1 Like

//Problem 1

console.log(a);
var a=12;

Answer:

undefined

  • var has global scope, thus output will be undefined even though the variable is declared later.

  • In case of let and const it would throw an error as both of them have local scope and cannot be used before declaring them first.

//Problem 2

console.log(Sum(2,3))
function Sum(a,b){
return a+b;
}

Answer:
5

functions have global scope, so we can call a function and then create it, it will still give the correct output.

//Problem 3

console.log(0/0)
Answer:
Though 0/0 is infinity in mathematics, the output we’ll get is NaN (not a number).

//Problem 4

const arr=[1,2,3,4,5,6,7,8]
console.log(arr.slice(1,-1))

Answer:
`[2,3,4,5,6,7]

slice() accepts two arguments, starting index (includes) and ending index(excludes).
Here the slicing begins at index 1 i.e 2 and ends at index -1 i.e last index of arr

1 Like