Sitemap
JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

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 3345
  • 1 + 2 is 3. Then 3 + "3" results in "33" (string concatenation starts here). After that, "33" + 4 and "334" + 5 continue as string concatenations.
console.log(false - 1);
// Output -1
  • false is converted to 0, so 0 - 1 is -1.
console.log(true + 1);
// Output 2
  • true is converted to 1, so 1 + 1 is 2.
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 122
  • 1 is 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 number 2. So, 1 + 2 is 3, which is then concatenated with "2", resulting in "32".
console.log(1 + -"1" + "2");
// Output 02
  • 1 is the number and — convert "1" to the number . So, 1–1 is 0, which is then concatenated with "2", resulting in "02".
console.log(1 + "1" + - "2"); 
//Output 11-2
  • 1 is 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 is 11-2.
console.log("A" - "B" + "2");
// Output Nan2
  • "A" - "B" results in NaN because subtraction between non-numeric strings is not defined. NaN + "2" treats NaN as a string, so it results in "NaN2".
console.log("A" - "B" + 2);
// Output Nan
  • "A" - "B" results in NaN for the same reason as above. NaN + 2 still results in NaN because any arithmetic operation with NaN returns NaN.
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 Function
  • sayHi.call(person, 23) calls the sayHi function immediately, setting this to person and passing 23 as age. The result is "Abdul Moiz is 23".
  • sayHi.bind(person, 23) creates a new bound function with this set to person and age fixed at 23. It does not call the function immediately, so it returns a bound function instead.
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 within foo)
  • b = 10; (creates a global variable b since it is not declared with var, let, or const)
  • The function x() {} declaration inside foo is hoisted, but it doesn't affect x outside foo.
  • After calling foo(), b is accessible globally with the value 10.
var info = {
name: "Ahsan Mumtaz",
getSecretIdentity: function () {
return this.name;
},
};

var stoleSecretIdentity = info.getSecretIdentity;

console.log(stoleSecretIdentity()); // undefined
console.log(info.getSecretIdentity()); // Ahsan Mumtaz
  • info.getSecretIdentity() works as expected because this refers to the info object, returning "Ahsan Mumtaz".
  • stoleSecretIdentity is a reference to info.getSecretIdentity but not bound to info. When called independently, this refers to the global scope (or undefined in strict mode), so this.name is undefined.
Press enter or click to view image in full size

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️!

--

--

Abdul Moiz
Abdul Moiz

Written by Abdul Moiz

A developer always struggling to code , part time reader . occasional writer—here when inspiration strikes. Let's learn and connect through bugs & books!"

JavaScript in Plain English
JavaScript in Plain English

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.