DEV Community

Code Craft-Fun with Javascript
Code Craft-Fun with Javascript

Posted on • Edited on

4 1 1 1 1

Javascript 【tricky】💡interview output questions (Part 1)

What is the output for each code mentioned below and Why ?

Add your solutions with explanation in the comment section.

Question 1

(function(){
  var variable1 = variable2 = 3;
})();
console.log("variable1 defined? " + (typeof variable1 !== 'undefined'));
console.log("variable2 defined? " + (typeof variable2 !== 'undefined'));
Enter fullscreen mode Exit fullscreen mode

Question 2

var myObject = {
    value: 1,
    printVal: function() {
        var self = this;
        console.log("printVal:  this.value = " + this.value);
        console.log("printVal:  self.value = " + self.value);
        (function() {
            console.log("inner printVal:  this.value = " + this.value);
            console.log("inner printVal:  self.value = " + self.value);
        }());
        } };
myObject.printVal();
Enter fullscreen mode Exit fullscreen mode

Question 3

console.log(0.1 + 0.2);
console.log(0.1 + 0.2 === 0.3);
Enter fullscreen mode Exit fullscreen mode

Question 4

for (var i = 0; i < 5; i++) {
  setTimeout(() => console.log(i), 1000 * i);
}
Enter fullscreen mode Exit fullscreen mode

Question 5

var arr1 = "john".split("");
var arr2 = arr1.reverse();

arr2.splice(arr2.length - 1, 1);

console.log("array 1: length=" + arr1.length + " last=" + arr1.slice(-1));
console.log("array 2: length=" + arr2.length + " last=" + arr2.slice(-1));
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (1)

Collapse
 
codecraftjs profile image
Code Craft-Fun with Javascript

Find the Solution here

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay