Giter Site home page Giter Site logo

ganqqwerty / 123-essential-javascript-interview-questions Goto Github PK

View Code? Open in Web Editor NEW
4.8K 133.0 1.2K 1.14 MB

JavaScript interview Questions

License: BSD 3-Clause "New" or "Revised" License

interview-questions javascript javascript-interview-question javascript-functions referenceerror inherited-properties frontend-interview coding-interviews coding-challenge coding-interview

123-essential-javascript-interview-questions's People

Contributors

akhil24-abd avatar alextaietti avatar anandshenoy14 avatar androidfanatic avatar chrisdiana avatar creator1134 avatar divyanshu-rawat avatar ganqqwerty avatar getmetorajesh avatar gitter-badger avatar givehug avatar harmansingha avatar intsrc avatar ironmaniiith avatar islamcodehood avatar kamleshpandey98 avatar lleel avatar malipramod avatar mdsaleemj avatar moshiurse avatar nishant8bits avatar omarelgabry avatar orinamio avatar otomer avatar rahuldbs avatar samarpanda avatar snollygolly avatar sumeyradavran avatar tianp avatar tmkuba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

123-essential-javascript-interview-questions's Issues

Same questions

Hello Nishant, Your repo helps me learn a lot about javascript,I found that question 32 and 33 are the same with questions 34 and 35,for telling you the problems and hope to learn more from you.

Question 29: check for null

In "Question 29. Write a function called deepClone which takes an object and creates a object copy of it.", the deepClone function should check for a null value:

if( typeof object[key] === 'object' && object[key] !== null )...

What's more, we usually want to also check for hasOwnProperty().

If condition statement evaluate using eval?

If condition statement evaluate using eval

I'm not sure if this is correct? According to mdn,

if (condition)
condition
An expression that evaluates to true or false.

My understand is function f() {} here is an function expression instead of function declaration so f is not a variable. Hence it's undefined.

Can you provide any reference of "If condition statement evaluate using eval"?

Question No.3 - Answer example incorrect

The function defined in the prototype function displayIncreasedSalary() is trying to call the private function increaseSalary() defined in the constructor function. This doesn't work.

Function part question 1: missing one output

The code is

function funcA(){
	console.log("funcA ", this);
	(function innerFuncA1(){
		console.log("innerFunc1", this);
		(function innerFunA11(){
			console.log("innerFunA11", this);
		})();
	})();
}
	
console.log(funcA());

The given answer funcA Window {...} innerFunc1 Window {...} innerFunA11 Window {...} didn't consider the output from the last line, which should be undefined

Question 12:

Now it does not return undefined x 1 but "empty"
Please check and revise the answer.

Question 38

Question 38 seems to be wrong.

emp1.displayName = function() {
console.log('xyz-Anonymous');
};

employee.displayName();// "xyz-Anonymous"
emp1.displayName(); // "Nishant"

These two should be switched.

employee.displayName();// "Nishant"
emp1.displayName(); // "xyz-Anonymous"

Coding Questions -> Arrays -> 2. What would be the output of following code?

(function() {
	var array1 = [];
	var array2 = new Array(100);
	var array3 = new Array(['1',2,'3',4,5.6]);
	console.log(array1);
	console.log(array2);
	console.log(array3);
	console.log(array3.length);
}());
  1. [] [] [Array[5]] 1
  2. [] [undefined × 100] Array[5] 5
  3. [] [] ['1',2,'3',4,5.6] 5
  4. [] [] [Array[5]] 5

Answer: 1) [] [] [Array[5]] 1

Correct is [] [undefined × 100] [Array[5]] 1

Question 38

employee.displayName(); //Nishant
emp1.displayName();//xyz-Anonymous

Could Use Improvement

I'd like to commend you for the compilation of the JavaScript examples. I like the variety you provide and they give some good reminders on common pitfalls. I do, however, implore you to review and update some of your examples. Some are inaccurate or have outright errors.

Question 48 for example assigns an object to a reserved word, location. And then the 'Method 1', ES6 example for this is not only a poor example of ES6, it is also errored. A good example would be:
const merge = (toObj, fromObj) => Object.assign(toObj, fromObj);

Keep up the good work. Just be sure that it is correct. :)

Question 2 answer is incorrect

var y = 1;
if (function f() {}) {
  y += typeof f;
}
console.log(y); // 1undefined

I do not agree with this answer as it does not explain what's going on here.

f is a named function, but it is "created" within the if statement itself, and the JS engine does NOT create a reference for it at the beginning of the scope.

For example:

(function(){
  console.log(typeof f, typeof y) // function undefined
  function f(){}
  var y = 1;
})()

As you can see the function is declared after the printing of the typeof f and this works because all variables and functions references are compiled to the beginning of the scope.

The above code is converted to obey the rules of JS engines:

(function(){
  var f = function f(){}, // not really, but "f" acts like a variable because this is only a reference to the named function
       y;  
  console.log(typeof f, typeof y); // function undefined
  y = 1;
})()

but in the question, the function was created within the if statement, and the JS engine therefor does not hoist the reference to the beginning of the scope, but instead the whole thing evaluates into something the if statement can handle, which is fundamental true/false scenario and then the was never really created and is discarded by GC.

Another interesting thing would be to print this:

console.log( function f(){}, (function f(){})() );  // ƒ f(){} undefined

and this also shoes that when functions are created inside specific "things" no reference is really created:

console.log( function f(){}, typeof f); // ƒ f(){} "undefined"

Anyway, I think this question should never be asked in an interview because this is far far from real-life scenarios and does not show any skills beside general knowledge of lesser-known specific hoisting-related scenarios.

Array question 2

In the Array topic, the answer to question2 is wrong.
It should be 2 i.e ([] [undefined × 100] Array[5] 1)

Question 23

Hey there, description in question 23 needs a bit of adjustment

Let say new User("xyz") created a object called foo and returned now foo["location"] would be assigned value as USA and person is referencing to foo["location"].

Here, person is not referencing foo["location"], instead it is being assigned the result of Assignment Operator = which in this case returns string 'USA'. Assignment operator always returns the value of the expression on the right, as described here:
ECMAScript Specification , pt 12.14.4

Otherwise it's totally correct, thanks for great work!

Question 31

Both compares return false. This is because in JavaScript we pass non primitive data types by reference. So, say:
{id:1} == {id:1} // returns false even though is shallow compare, we are not comparing here two objects values, but 2 references, which are different.

function is not a basic type

Question 20. What’s the difference between typeof and instanceof?
The typeof operator checks if a value belongs to one of the seven basic types: number, string, boolean, object, function, undefined or Symbol.
image

Explanation of "Return Statement" #7 is misleading

Thank you so much for your list of helpful questions.

Looking at https://github.com/ganqqwerty/123-Essential-JavaScript-Interview-Questions#7-what-would-be-the-output-of-following-code--2, I think the explanation would make more sense like this:

(function () {
  console.clear();
  function getName1() {
    console.log("getName1", this.name);
  }
  Object.prototype.getName2 = function () {
    console.log("Object.prototype.getName2", this.name);
  };
  Object.prototype.getName3 = () => {
    console.log(
      "Object.prototype.getName3",
      this.name,
      "Why does this one not work?"
    ); //https://stackoverflow.com/a/31755263/470749 because "Arrow functions provide a lexical this. It uses the `this` that is available at the time the function is evaluated." See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
  };
  Object.prototype.getName4 = () => {
    console.log(
      "Object.prototype.getName4",
      Object.getPrototypeOf(this).name,
      "This one does not make sense"
    );
  };
  let personObj = {
    name: "Tony",
    print: getName1
  };
  personObj.print();
  personObj.getName2();
  personObj.getName3();
  personObj.getName4();
})();

Thanks again, and best of luck.

Question 2, 12, 15

Question 2

var y = 1;
if (function f(){}) {
    y += typeof f;
}
console.log(y);

I got 1object instead of 1undefined, might be related to this issue

Question 12

var trees = ["redwood", "bay", "cedar", "oak", "maple"];
delete trees[3];
console.log(trees); // ["redwood", "bay", "cedar", empty, "maple"]; //trees[3] is empty instead of undefined on chrome

Question 15

Here associativity of the assignment operator is Right to Left so first typeof y will evaluate first which is undefined and assigned to z and then y would be assigned the value of z and then z would be assign value 1.

z should be assign the value of y which is undefined

"Answer" Question 3 is misleading -- and potentially wrong.

Question 3. What is the drawback of creating true private in JavaScript?

The "solution" says each copy of Employee will have its own copy of the increaseSalary and that such an implementation would be poor for memory. While that is true, it is also true that each instance of Employee would also have it's own copy of any properties or methods added to this. So in the case of your example, for the purposes of memory, increaseSalary is no different from dispalyIncreasedSalary in terms of memory consumption.

To actually be more memory efficient, one would want to put common properties and methods on the Employee prototype.

https://stackoverflow.com/questions/8433459/what-s-the-purpose-of-prototype

Q31 Loose object equality

In Q31, we have

function Clone(object){
  var newObject = {};
  for(var key in object){
  	newObject[key] = object[key];
  }
  return newObject;
}

var objectLit = {foo : 'Bar'}; 
	var cloneObj = Clone(obj); // Clone is the function which you have to write 
	console.log(cloneObj === Clone(objectLit)); // this should return false
	console.log(cloneObj == Clone(objectLit)); // this should return true
  1. where does the obj come from in var cloneObj = Clone(obj)?

  2. should A == B be the same as A === B when both A and B are objects? why the last two lines have different outputs?

Thanks.

Question 48

I think the variable ‘location‘ may need to change a value,otherwise the page will redirect.

Question 19: maybe add one more example

I debug this question, find it will be more interesting to add the following example:

var salary = "1000$";

(function () {
  console.log("Original salary was " + salary);

  let salary = "5000$";

  console.log("My New Salary " + salary);
})();

this example can emphasis the concept of 'hoist'. Somebody say 'let' or 'const' has no hoisting. But in fact it's not.

Question 49

Hi, thanks for the questionnaire!

A small correction:

// Create non-enumerable property
Object.defineProperty(person, 'phoneNo',{
	value : '8888888888',
	enumerable: false
})

Changing non-enumerable property value will return error in strict mode. In non-strict mode it won't through any error but it won't change the value of phoneNo.

Value of phoneNo won't change because 'writable' descriptor is false by default, not because 'enumerable' is false.

If you had:

Object.defineProperty(person, 'phoneNo',{
	value : '8888888888',
	enumerable: false,
        writable: true,
})

you would be able to change property value.

Questions 30 and 32 are the same.

Hi Nishant! I am really learning lots from your repo, but I noticed that questions 30 and 32 in the readme, and the exact same question repeated.

Just letting you know, everything else is amazing!

Question 48

looks like mistake:
Method 1: Using ES6, Object assign method
screenshot 2018-10-25 at 11 12 02

Is this repo still active?

I couldn't find much activity in this repository for the last few months. There are quite a few issues and pull requests pending, which didn't receive any attention.

@nishant8BITS What are your thoughts regarding this? I understand that this was also meant to be released as a commercial project, and you are probably unable to spend more time on the repo.

I consider this project to be a very important resource for interviews and have greatly benefited from it. I would be really happy to help out with the maintenance if you would like that.

Question 2 function expression

For the function expression in Q2

var myFunc1 = new function() {}
var myFunc2 = new function whatever() {} // whatever is not a variable here

I think it would be better to use something like the code below to distinguish the 'function' and the 'Function' object

var myFunc1 = function() {}
var myFunc2 = function whatever() {}
var myFunc3 = new Function ('a', 'b', 'return a+b')

Sorry, I don't understand why using new function(...) {...} rather than new Function()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.