Skip to main content


Showing posts with the label Javascript

Javascript Closure Interview Questions and Answers - 6

Here in the post, I have posted all the possible interview questions and answers on closure. If you are not familiar with the closure , I would highly recommend you to go to my post on   closure . Once you are comfortable with the topic, you can attempt these questions. All the answers are given at the end of the post. //Question 1 function foo() { var a = 5 function bar() { console.log(a) } return bar; } var baz = foo() baz()   // Question 2 function outer(a) { var b = 2 function inner() { var c = 5 console.log(a * b * c) } return inner; } var multiply = outer(5) multiply() // Question 3 const arr = [10, 20, 30, 40, 50] for (var i = 0; i < arr.length; i++) { setTimeout(function() { console.log('Index: ' + i + ', element: ' + arr[i]) }, 1000) } // Question 4 for (var i = 0; i < 5; i++) { setTimeout(function(i) { return function() {

ES 6 Interview Question and Answer - Intermediate Level

The interview questions posted in the article are based on let , rest/spread , default parameter, destructuring, object/template literals , etc introduced in ES 6. Find the output of the code in each question.   // Question 1 let a = 2; if (a > 1) { let b = a * 3; console.log(b); for (let i = a; i <= b; i++) { let j = i + 10; console.log(j); } let c = a + b; console.log(c); }   // Question 2 function foo() { console.log(a); console.log(b); var a; let b; } foo();   //Question 3 var funcs = []; for (let i = 0; i < 5; i++) { funcs.push(function() { console.log(i); }); } funcs[3](); //Question 4 var a = [2, 3, 4]; var b = [1, ...a, 5]; console.log(b); //Question 5 function foo(x, y, ...z) { console.log(x, y, z); } foo(1, 2, 3, 4, 5); //Question 6   function foo(x = 11, y = 31) { console.log(x + y); } foo();

Know Everything about Objects in Javascript - Basics

As we know in Javascript there are two different datatypes - Primitive Datatype like string, number, boolean, undefined, null, Symbol(introduced in ES6) and Reference Datatype like object. Primitive datatypes are immutable(cannot be changed) and values contain only a single thing (a string or a number etc) whereas Objects are mutable(can be changed). What is Object? An object is an unordered list of data types, stored as name-value(or key-value) pairs. Each item in the list is called a property (and functions are called methods).e.g. let person = {name : "Alex",  age: 27 }; console.log(person);  output : { name : "Alex" , age : 27 } age :  27 name :  " Alex " __proto__ :  Object We can also create the same person object differently let person = new Object(); = "Alex"; person.age = 27; console.log(person); output: { name : "Alex" , age : 27 } Generally, property names are kept as string but it ca

Making Curry in Javascript

In this post, we will try to understand Currying in Javascript, how we can make curry function and the reason behind the currying concept. What is Currying in Javascript? Currying is the process of taking a function with multiple arguments and turning it into a sequence of functions each with a single argument and eventually resolve to a value. Not clear with the definition,right? Lets understand with example, in which we have written a function to find the volume. // volume function definition using arrow const volume = (l, b, h) => (l * b * h);  // calling volume function volume(3,2,4);   // 24 Learn about arrow functions, here . Now here, when we want to call this volume function, we need to pass all the three arguments at one time. Consider a scenario where you get length, breadth and  height at different part of the function or javascript file. How can we achieve this? To answer such scenario, we can use currying in javascript. Making curry function

Come Closure in Javascript

What is a closure? If we want to derive a definition,we can say, A closure is a feature of Javascript where inner function has access to the outer (enclosing) function’s variables—scope chain. This scope chains means inner function has: access to its own scope (variables defined between its curly brackets and its parameters), access to the outer function’s variables and parameters,  access to the global variables. Now we will look into it in detail. Consider following example var globalVar = "xyz"; function outerFunc(outerArg) { var outerVar = 'Alex'; function innerFunc(innerArg) { var innerVar = 'Chris'; console.log("globalVar = " + globalVar); console.log("outerArg = " + outerArg); console.log("innerArg = " + innerArg); console.log("outerVar = " + outerVar); console.log("innerVar = " + innerVar); } innerFunc(456); } outerFunc(12

Javascript Interview Question and Answer - 4

In this blog, we will see 10 very simple but must know interview Question and answers. 1. What is the relation between JavaScript and ECMA Script? Javascript is a scripting language used for web or webapplication development whereas ECMA Script is a  like a guideline and rules for Javascript. 2. What is Callback? A callback is a JavaScript function passed to some other function as an argument which is to be executed after the main function has finished executing. function callbackFun(x) {     console.log(x);    }  function mainFun(num, callback) {     console.log("Hi I am going to call callback");    callback(num);  }  mainFun(7, callbackFun); 3. What is Closure? A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function's variables. Learn more about closure . 4. What is namespace in JavaScript? Namespace is used for grouping the desired functions, variables etc. under a unique name. This impro

The Class in Javascript - ES 6

In Javascript ES 5,we didn't have class as other languages like Java or C#. However, we use to create a function, in such a way, that it will act as a class. e.g. function Person(name,age) { = name;        this.age = age;  }  var person1 = new Person("Alex", 27); console.log(person1); Person  { name : "Alex" , age : 27 } age :  27 name :  " Alex " __proto__ :  Object We already had prototype-based inheritance for class, provided by functions, in javascript. The JavaScript classes, introduced in ES 6, are primarily syntactical sugar over this. The class syntax does not introduce a new object-oriented inheritance model to JavaScript. That means, JavaScript class is a type of function. Classes are declared with the class keyword. We usually write class name in Pascal-caseing. Like function, classes can be written in two ways, class declaration and class expression. Class Declaration  We use function expressi

Making Arrow in Javascript - ES 6

Arrow function aka fat arrrow was intorduced in ES6 as a new syntax for writing Javascript function.This is the most popular and highly used ES6 feature. Source Ponyfoo What is Arrow function? Arrow functions provide a different way of creating functions in JavaScript. Besides a shorter syntax, they offer advantages when it comes to keeping the scope of the this keyword. What I meant by this line is, unlike other functions, the value of this inside arrow functions is not dependent on how they are invoked or how they are defined.It depends only on its enclosing context. // ES5 - function declaration function callMe(name){              console.log(name);  } which you could also write as:  // ES5 - function expression const callMe = function(name) {         console.log(name); } becomes:  // ES6 const callMe = (name) => {              console.log(name);  } Important Uses: When having no arguments , you have to use empty parentheses in the function de