Quick Js Operations — Javascript
Interviewers sometimes throw in quick, unexpected JavaScript questions to gauge a candidate’s presence of mind and ability to think on their feet. These questions may seem straightforward in hindsight, but in the moment, they can catch us off guard — especially if we panic or overthink.
Since we’re diving into advanced JavaScript concepts and interview preparation, here’s a list of commonly asked quick JavaScript operation questions that can help you respond confidently when the pressure is on.
console.log(1 + 2 + 3);
// OutPut 6- all operands are numbers, so they are added normally
console.log("1" + "2" + "3");
// Output 123- All operands are strings, so the
+operator concatenates them in sequence.
console.log(1 + 2 + "3");
// Output 33 - 1 + 2 = 3 because they are treated as number and 3 + “3” concatenate because of string
console.log(1 + "2" + "2");
// Output 122- All operands are strings, so the
+operator concatenates them in sequence:"1" + "2"results in"12", and"12" + "2"results in"122".
console.log(1 + 2 + "3" + 4 + 5);
// Output 33451 + 2is3. Then3 + "3"results in"33"(string concatenation starts here). After that,"33" + 4and"334" + 5continue as string concatenations.
console.log(false - 1);
// Output -1falseis converted to0, so0 - 1is-1.
console.log(true + 1);
// Output 2trueis converted to1, so1 + 1is2.
console.log([1] + "abc");
// Output 1abc[1]is converted to the string'1'(by.toString()), and then concatenated with"abc".
console.log(1 + "2" + + "2");
// Output 1221is converted to'1', so'1' + "2"results in"12", then"12" + "2"results in"122".
console.log(1 + +"2" + "2");
// Output 32+"2"converts the string"2"to the number2. So,1 + 2is3, which is then concatenated with"2", resulting in"32".
console.log(1 + -"1" + "2");
// Output 021is the number and — convert"1"to the number . So,1–1is0, which is then concatenated with"2", resulting in"02".
console.log(1 + "1" + - "2");
//Output 11-21is the number and"1"is string so they are concatenated to each other & ,"2"is converted to a number because of the — operator , So the output is11-2.
console.log("A" - "B" + "2");
// Output Nan2"A" - "B"results inNaNbecause subtraction between non-numeric strings is not defined.NaN + "2"treatsNaNas a string, so it results in"NaN2".
console.log("A" - "B" + 2);
// Output Nan"A" - "B"results inNaNfor the same reason as above.NaN + 2still results inNaNbecause any arithmetic operation withNaNreturnsNaN.
const obj = { a: "one", b: "two", a: "three" };
console.log(obj);
// Output b= two , a = "three- When you create an object with duplicate keys, the last occurrence of the key overwrites any previous values.
- This behavior occurs because JavaScript objects do not support duplicate keys, so the last value assigned to a key is the one that persists.
const person = { " Abdul Moiz" };
function sayHi(age){
return '$this.name) is $(age)';
}
console.log(sayHi.call(person , 23)); // Output Abdul Moiz is 23
console.log(sayHi.bind(person , 23)); // Bound FunctionsayHi.call(person, 23)calls thesayHifunction immediately, settingthistopersonand passing23asage. The result is"Abdul Moiz is 23".sayHi.bind(person, 23)creates a new bound function withthisset topersonandagefixed at23. It does not call the function immediately, so it returns abound functioninstead.
var x = 1;
function foo() {
var x = b = 10;
return;
function x() {}
}
foo();
console.log(b); // Output 10- Inside
foo,var x = b = 10;is interpreted as: var x = 10;(local variable withinfoo)b = 10;(creates a global variablebsince it is not declared withvar,let, orconst)- The
function x() {}declaration insidefoois hoisted, but it doesn't affectxoutsidefoo. - After calling
foo(),bis accessible globally with the value10.
var info = {
name: "Ahsan Mumtaz",
getSecretIdentity: function () {
return this.name;
},
};
var stoleSecretIdentity = info.getSecretIdentity;
console.log(stoleSecretIdentity()); // undefined
console.log(info.getSecretIdentity()); // Ahsan Mumtazinfo.getSecretIdentity()works as expected becausethisrefers to theinfoobject, returning"Ahsan Mumtaz".stoleSecretIdentityis a reference toinfo.getSecretIdentitybut not bound toinfo. When called independently,thisrefers to the global scope (orundefinedin strict mode), sothis.nameisundefined.
A message from our Founder
Hey, Sunil here. I wanted to take a moment to thank you for reading until the end and for being a part of this community.
Get Abdul Moiz’s stories in your inbox
Join Medium for free to get updates from this writer.
Did you know that our team run these publications as a volunteer effort to over 3.5m monthly readers? We don’t receive any funding, we do this to support the community. ❤️
If you want to show some love, please take a moment to follow me on LinkedIn, TikTok, Instagram. You can also subscribe to our weekly newsletter.
And before you go, don’t forget to clap and follow the writer️!










