Javascript trick questions

There’s lot tricky test question if you go through interview, here I put some tricky and might be simple but we might not conscience that basic method and lost the point while interview:

Basic concept questions:

What does “use strict” do?

The “use strict” literal is entered at the top of a JavaScript program or at the top of a function and it helps you write safer JavaScript code by throwing an error if a global variable is created by mistake. For example, the following program will throw an error:

function doSomething(val) {
  "use strict"; 
  x = val + 10;
}

once you add “use strict”, it will through an error: x is not declare as a variable even though this will not cause code error once it is show undefined, and follow is correct version:

function doSomething(val) {
  "use strict"; 
  var x = val + 10;
}

What is the difference between == and ===?
The difference is that == performs implicit type conversion to check if values are equal to each other. So it will convert the values to true or false boolean values and then compare them. Below are some weird results of this implicit type conversion using the double equal operator:

"1" == 1 // true
true == 1 // true
[] == false // true
"" == false // true
undefined == null // true

This can really cause some problems when you’re writing JavaScript, so the safe thing to do is use the triple equal operator instead, which checks that the two values are of the same type and does not perform type conversion.

How would you check if a number is an integer?

A very simply way to check if a number is a decimal or integer is to see if there is a remainder left when you divide by 1.

function isInt(num) {
  return num % 1 === 0;
}

console.log(isInt(4)); // true
console.log(isInt(12.2)); // false
console.log(isInt(0.3)); // false

What’s the difference between undefined and not defined in JavaScript

You declare JavaScript variables with the var keyword: var carname;
After the declaration, the variable is empty (it has no value).
To assign a value to the variable, use the equal sign var carname=”Volvo”;
In computer programs, variables are often declared without a value. The value can be something that has to be calculated, or something that will be provided later, like user input. Variable declared without a value will have the value undefined.
The variable carname will have the value undefined after the execution of the following statement: var carname;
var hoisting
In JavaScript, a variable can be declared after being used.

var x; // Declaration
typeof x === 'undefined'; // Will return true

What is “closure” in javascript? Can you provide an example?
A closure is a function defined inside another function (called parent function) and has access to the variable which is declared and defined in parent function scope.

The closure has access to the variable in three scopes:

Variable declared in his own scope
Variable declared in parent function scope
Variable declared in the global namespace

function multiply(a) {
  return function(b) {
    return a * b;
  }
}

multiply(5)(6);

How to check if an object is an array or not?

The best way to find whether an object is instance of a particular class or not using toString method from Object.prototype

if(Object.prototype.toString.call(arrayList) === '[object Array]') {
  console.log('Array!');
}

what is function hoisting in JavaScript?
In JavaScript var-declared variables and functions are hoisted. Let’s take function hoisting first. Basically, the JavaScript interpreter looks ahead to find all the variable declaration and hoists them to the top of the function where it’s declared.

foo(); // Here foo is still undefined
var foo = function foo() {
  return 12;
};
var foo = undefined;
foo = function foo() {
  // Some code stuff
}
foo(); // Now foo is defined here

Explain what a callback function is and provide a simple example.
A callback function is a function that is passed to another function as an argument and is executed after some operation has been completed. Below is an example of a simple callback function that logs to the console after some operations have been completed.

function modifyArray(arr, callback) {
  // do something to arr here
  arr.push(100);
  // then execute the callback function that was passed
  callback();
}

var arr = [1, 2, 3, 4, 5];

modifyArray(arr, function() {
  console.log("array has been modified", arr);
});

And also there’s lot test javascript question quiz on line