Guess Output of JS programs

Image for post
Image for post

📄 Table of contents

// IIFE
(function(){
let a = b = 100;
})();
console.log(b); // 100
console.log(a); // "ReferenceError: a is not defined
// IIFE
(function(){
var b = 101;
let a = 100;
})();
console.log(b); // "ReferenceError: b is not defined "
console.log(a); // will not reach here
Image for post
Image for post

Post increment will return your value before incrementing thats why we are getting 0 all the times. We should use ++num which will return after incrementing and will output 1,2,3.

LogMeIn

error boundaries in reactWrite a JavaScript program to find the most frequent item of an array.
search should be case insensitive.
input: [1, 'T', 'a', 'T', 2, 3, 'T', 3, 't', 2, 't', 9, 3];
output: t = 5
let tempArray = [1, 2, 3, 4, 5];
let averageValues = tempArray.average();
console.log(averageValues); // should return average of tempArray
const a = [1,2,3];
const b = [1,2,3];
const c = “1,2,3”;
console.log(a === b); // false
console.log(a === c); // false
// convert [1,2,3] to String
[1,2,3] + ""
// "1,2,3"
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
# undefined▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬function parent() {
var hoisted = "a variable";

function hoisted() {
return "a function";
}
return hoisted();
}
console.log(parent());
Output: # TypeError: hoisted is not a functionVariable assignments > function declarations > variable declarations▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬function containingFunction() {
var hoistedVariable = 2 + 2;
function hoistedFunction() {
return hoistedVariable;
}
return hoistedFunction();
}
containingFunction()
# 4let arr = [4, 4];for (x of arr){
console.log(x); // output => 4,4
}
for (x of arr.keys()) {
console.log(x); // output = 🤔 ?
}
Image for post
Image for post

The above code will output the following to the console:

outer func:  this.foo = bar
outer func: self.foo = bar
inner func: this.foo = undefined
inner func: self.foo = bar
function f1(arr) {
arr.push(7);
}
function f1(arr) {
arr.push(8);
}
var arr = [6];
f1(arr);
console.log(arr.join(","));
# 6,8var count1 = 0, count2 = 0;
for (var i = 0; i < 3; i++) {
try {
if (i === 2) {
console.log("inside try statement");
}
}
catch (e) {
count1++;
} finally {
count2++;
}
}
console.log(count1, count2);
# inside try statement
# 0 3
(function () {
var a = 4;
function f1() {
var a = 5;
}
if (true) {
var a = 6;
}
f1();
console.log(a);
})()
# 6function Base2() {
this.num = 2;
}
Base2.prototype = {
base_get_num: function () {
return this.num;
}
}
function Derived2() {}
Derived2.prototype = Base2.prototype;
var d = new Derived2();
console.log(d.base_get_num());
# undefinedfunction Base2() {
this.num = 2;
}
Base2.prototype = {
num: 4,
base_get_num: function () {
return this.num;
}
}
function Derived2() { }
Derived2.prototype = Base2.prototype;
var d = new Derived2();
console.log(d.base_get_num());
# 4function Base3() {
this.num = 2;
}
Base3.prototype = {
num: 4,
base_get_num: function () {
return this.num;
}
}
function Derived3() {
Base3.apply(this, arguments);
}
var d = new Derived3(2);function bar(){
return foo;
foo =10;
var foo = "11";
function foo() {}
}
alert(typeof bar()); # function

Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store