
If you’re searching for javascript code-based questions for interview observe or brainstorming then I might say you’re on the proper place to study. I’ll attempt to cowl all tough and key questions which aren’t a part of the theoretical Javascript. For higher understanding, I might advocate taking a pen and paper and attempting to unravel it first, might be a superb begin.
1. What would be the output of the beneath code traces?
console.log("11"-1); // 10console.log("11"+1); // "111"console.log(0 === [[[0]]]); // falseconsole.log(0 == [[[0]]]); // trueconsole.log(0 === "0"); // falseconsole.log(0 == "0"); // trueconsole.log([1,[2,[3,[4,[5]]]]].toString()) // 1,2,3,4,5
2. Tips on how to convert the multi-dimension array right into a 1-D array effectively?
const arr = [1,[2,[3,[4,[5]]]]]
const dArray = arr.toString().cut up(',').map((num) => Quantity(num))
The take a look at array is said to check our code within the first line. Within the subsequent line, we transformed the take a look at array right into a string format after our end result seems like ‘1,2,3,4,5’. the ultimate step can be splitting based mostly on ‘,’ and changing to a quantity.
3. Create 1 to 10 quantity guarantees with random delay and Print them into steady sequence of 1 to 10.
const delay = (val, ms) => new Promise(resolve => setTimeout(resolve(val), ms));const promiseArr = []
for(let i=1; i<=10;i++)
promiseArr.push(delay(i, Math.random()%5));
const end result = Promise.all(promiseArr)
end result.then(res =>
console.log(res) // [1,2,3,4,5,6,7,8,9,10]
)
delay operate creates a brand new promise with a given worth and delay.
The Promise.all() technique takes an iterable of guarantees as an enter and returns a single Promise that resolves to an array of the outcomes of the enter guarantees. This returned promise might be fulfilled when all the enter’s guarantees have been fulfilled, or if the enter iterable comprises no guarantees. It rejects instantly upon any of the enter guarantees rejecting or non-promises throwing an error and can reject with this primary rejection message/error.
4. Tips on how to examine whether or not the thing worth is an object or not?
typeof yourVariable === 'object' && yourVariable !== null
5. Tips on how to create a customized swap in Javascript?
const match = (expr, allCases) => const allCases =
Hello: 'good morning',
Purple: "colour",
BMW: "my favorite",
default: "not out there"
const matched = match('Hello', allCases)
const notMatched = match('Not', allCases)console.log(matched) // Good Morning
console.log(notMatched) // not out there
match is a operate which takes all instances and your expression to match in case your expression doesn’t exist then it’s going to discover the default one. allCases is an object of key worth which include our all instances.
6. What would be the output of the beneath code?
const a = Quantity(1)
const b = Quantity(2)
const c = "1"
const d = "2"console.log(typeof a * b) // NaN
console.log(typeof c * d) // NaN
console.log(typeof a * c) // NaN
console.log((a*b) == (c*d)) // true
console.log((a*b) === (c*d)) // true
console.log(typeof Quantity(a * b)) // quantity
console.log(typeof Quantity(c * d)) // quantity
7. What would be the output of the beneath code?
const arr = [1,2,3,4]
arr.size = 0; // do not do that in your lifeconsole.log(arr) // []console.log("1" - "2" + "3" + "4" * "5")
// first * happens and end result might be
// "1" - "2" + "3" + "20"
// now left to proper move goes
// "-1" + "3" + "20"
// "-1320"
8. We have now three guarantees with the identify and delay, (sluggish, 200), (fast, 50), (immediate, 0), and all ought to resolve on the similar time and order ought to preserve into sluggish, fast, and immediate sequence.
const sluggish = new Promise(resolve =>
setTimeout(resolve, 200, 'sluggish');
);
const immediate = new Promise(resolve =>
setTimeout(resolve, 0, 'immediate');
); ;
const fast = new Promise(resolve =>
setTimeout(resolve, 50, 'fast');
);const end result = Promise.all([slow, quick, instant])end result.then((res) =>
console.log(res)
)
9. What would be the output of the beneath code?
var a=identify:"RKstar"
var b=identify:"RKstar"console.log(a===b); // false
console.log(a==b); // false
10. If the array is an occasion of the thing then how will you differentiate an object or array? How a program to make distinction between them?
console.log(diff([])) // Array
console.log(diff()) // Object
console.log(diff(null)) // Null or Undefinedoperate diff(obj)
if(Object.prototype.toString.name(obj) === '[object Array]')
return 'Array'
else if(typeof obj === 'object' && obj !== null)
return 'Object'
else
return 'Null or Undefined'
Some Theoretical and Code-based questions for You, please remark your resolution right here.
what’s the distinction between null and undefined?
Write a JSON parser, can not use inbuilt JSON parser.