Skip to content

Instantly share code, notes, and snippets.

@Buggytheclown
Last active April 10, 2025 18:24
Show Gist options
  • Select an option

  • Save Buggytheclown/d54c7b2f0c190dc9c9f8016a7991d4dc to your computer and use it in GitHub Desktop.

Select an option

Save Buggytheclown/d54c7b2f0c190dc9c9f8016a7991d4dc to your computer and use it in GitHub Desktop.
Basic javascript interview questions. (Some 'bad' working scripts)
/*
* IIFE
* https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
*/
(function() {
})();
/*
* Comparison operators
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
*/
function myEq(obj1, obj2) {
return obj1 === obj2;
}
console.assert(myEq([1, 2], [1, 2]))
/*
* Context
* https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/this
*/
(function() {
var bob = {
name: 'Bob',
// sayName: () => {
sayName: function() {
return this.name;
},
};
var alice = {
name: 'Alice',
};
var bobSayName = bob.sayName;
console.assert(bobSayName() === 'Bob'); // 1
alice.bobSayName = bobSayName;
console.assert(alice.bobSayName() === 'Alice'); // 2
console.assert(bobSayName.call(alice) === 'Alice'); // 3
console.assert(bobSayName.bind(bob).call(alice) === 'Alice'); // 4
})();
/*
* Closure
* https://developer.mozilla.org/en/docs/Web/JavaScript/Closures
*/
(function() {
var array = [1, 2, 3, 4];
for (var index = 0; index < array.length; index++) {
setTimeout(function() {
console.assert(typeof array[index] === 'number');
}, 0);
}
})();
// reference
(function() {
var arr1 = [1, 2, 3, 4];
var arr2 = arr1;
arr2.push(5);
console.assert(myEq(arr1, [1, 2, 3, 4]));
console.assert(myEq(arr2, [1, 2, 3, 4, 5]));
console.assert(arr1 !== arr2);
})();
/*
* Asynchronous single threaded
* https://developer.mozilla.org/en/docs/Web/JavaScript/EventLoop
*/
(function() {
var stack = [];
stack.push(1);
setTimeout(() => stack.push(2), 1000);
setTimeout(() => stack.push(3), 0);
Promise.resolve(4).then(stack.push.bind(stack));
stack.push(5);
setTimeout(() => {
console.assert(myEq(stack, [1, 2, 3, 4, 5]));
}, 5000);
})();
/*
* Type checking
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
*
* Types
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
*/
(function() {
const isCorrectlyCompared = (
typeof undefined === 'undefined' &&
typeof null === 'null' &&
typeof true === 'boolean' &&
typeof 42 === 'number' &&
typeof 42n === 'number' &&
typeof '42' === 'string' &&
typeof Symbol('42') === 'symbol' &&
typeof Function === 'function' &&
typeof [] === 'array' &&
typeof {} === 'object'
);
console.assert(isCorrectlyCompared === true);
})()
/*
* Hoisting
* https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
*
* Temporal Dead Zone
* https://stackoverflow.com/questions/33198849/what-is-the-temporal-dead-zone/33198850#33198850
*/
(function() {
let test = 'outer';
someFunction();
const test2 = 'outer2';
function someFunction() {
console.assert(test === 'outer');
console.assert(test2 === 'outer2');
var test = 'inner';
}
})();
/*
* Object's properties, prototype
* https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
* https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype
*/
(function() {
var myObj = { myProp: 123 };
var myObjProperties = [];
for (var prop in myObj) {
myObjProperties.push(prop);
}
console.assert('toString' in myObj);
console.assert(myObjProperties.includes('toString'));
})();
/*
* Scope
* https://developer.mozilla.org/en-US/docs/Glossary/Scope
*
* Block-scoped variable
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
*/
(function() {
for (var vari = 5; vari > 0; vari--) {
}
for (let leti = 5; leti > 0; --leti) {
}
{
const consti = 5;
}
console.assert(myEq([vari, leti, consti]), [0, 0, 5]);
})();
/*
* Promise
* https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
*/
(function() {
const stack = [];
new Promise(function(res, rej) {
throw 'My-Exeption';
})
.catch(function(ex) {
stack.push('catched');
})
.then(
function(res) {
stack.push('then');
},
function(rej) {
stack.push('catched');
},
);
setTimeout(() => console.assert(myEq(stack, ['catched', 'catched'])), 1000);
})();
/*
* async/await
* https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
*/
(async function(){
async function x1(){return 42}
async function x2(){return await 42}
async function x3(){return Promise.resolve(42)}
async function x4(){return await Promise.resolve(42)}
console.assert(await x1() === await x2());
console.assert(await x1() === await x3());
console.assert(await x1() === await x4());
x1().then(async (res1) => console.assert(res1 === await x1()));
})()
(async function () {
async function asyncThrowError() {
throw new Error(42);
}
function throwError() {
throw new Error(42);
}
async function x1() {
try {
return throwError();
} catch (e) {
return 'caught';
}
}
async function x2() {
try {
return await throwError();
} catch (e) {
return 'caught';
}
}
async function x3() {
try {
return asyncThrowError();
} catch (e) {
return 'caught';
}
}
async function x4() {
try {
return await asyncThrowError();
} catch (e) {
return 'caught';
}
}
console.assert((await x1()) === (await x2()));
console.assert((await x1()) === (await x3()));
console.assert((await x1()) === (await x4()));
})();
/*
* Inheritance
* https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance
* https://gist.github.com/Buggytheclown/ade9c337fc8869e3a9d3a43b195519c2
*
* fix implementations:
*/
/*
class CA {
static staticMethodA() {
return 42;
}
constructor(name = 'default name') {
this.name = name;
}
methodA() {
return this.name;
}
}
class CB extends CA {
methodB() {
return 43;
}
}
*/
(function() {
function CA(name = 'default name') {
this.name = name;
}
CA.prototype.methodA = () => {
return this.name;
}
function CB() {}
CB.prototype = CA.prototype;
CB.prototype.methodB = () => {
return 43;
};
})();
/*
* Call stack
* https://developer.mozilla.org/en-US/docs/Glossary/Call_stack
* http://www.2ality.com/2015/06/tail-call-optimization.html
*
* https://en.wikipedia.org/wiki/Trampoline_(computing)
*/
// Recursive sums of the First n Natural Numbers
(function() {
function sums(n) {
if (n === 0) {
return 0;
} else {
return n + sums(n - 1);
}
}
console.log(sums(30000));
})()
@ArturJS
Copy link

ArturJS commented Feb 13, 2020

https://habr.com/ru/post/486820 - one more that's also worth saying about =)

@alocvm
Copy link

alocvm commented Oct 21, 2021

Could you please include this question:

https://stackoverflow.com/questions/40884153/try-catch-blocks-with-async-await

(async function () {
    try {
        return throwError(); 
    } catch (err) {
        console.log(err);
    }

    async function throwError() {
        throw new Error('My Error');
    }
})()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment