Skip to main content


Showing posts from April, 2019

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

How to Spread in Javascript - ES 6

Spread syntax was introduced as part of Javascript in the ES2015/ECMAS-6. This allows iterable likes array, object or string to get expanded. Spread can be used in following ways: in array : [...iterableArray, 3 , 'four', 5] in object : {...iterableObj} The spread operator is very useful for concat, copying,apply, expanding, math operations,etc in array and objects. Let discuss each in detail. Concat var arr1 = [1,2,3]; var arr2 = [4,5,6]; without spread var concatenatedArray = arr1.concat(arr2); // [1,2,3,4,5,6] with spread var concatenatedArray = [...arr1, ...arr2 ] // [1,2,3,4,5,6] Copying var arr1 = [1,2,3]; var arr2 = [...arr1]; arr2.push(4); console.log(arr2); // [1,2,3,4] console.log(arr1); // [1,2,3] Replacement for apply var add = (a,b,c) => a+b+c; var arr1 = [1,2,3]; add(...arr1); // replacing add.apply(null,arr1); Expanding array var programming = ["Java", "NodeJs", "Python"]; var sub

Javascript Interview Question and Answer - 3

In this post, I have discussed 5 basic javascript question and answer that is mostly asked in the interviews. Most of the time interviewer starts the interview with some of these basic questions. So here we have Q1. What is the difference between == and ===? Solution  : When we want to test the equality of two values(LHS and RHS), we use == or ===. == is basically used to check if the two values are equal or not,and we are not considering their datatype. e.g. if we check 5 == "5"   // true as values are equal undefined = null   //  true as both represents NO VALUE In case of ===, we not only consider the values but the datatype as well.e.g. 5 === "5"   // false as though values are equal but datatypes are different Similarly undefined === null   //  false, as datatype of undefined and null is undefined and object respectively It is highly recommended to use === in the programming as we get very unexpected results out of ==. e.g. false == 0   /

Setup for Angular 7 application

Before you start an angular project, we need to set up an environment for angular development. We need to have NodeJs and NPM (node package manager)  in our system. It is not necessary for Angular development but it makes the development easy. NPM is used to download the libraries and packages we need for the development. You can check the version of node and npm avaible in your system through command prompt or terminal. Here is the command. for node version:   node -v  for npm version:    npm -v GC02PH6MAG3QDE:my_book himanshusharma$ node -v v10.14.0 GC02PH6MAG3QDE:my_book himanshusharma$ npm -v 6.7.0 In my system, current version of node is 10.14.0 and npm is 6.7.0 when I am writing this article, yours can be different. Now we need to install angular/cli using npm. Angular/cli makes it easy to create a angular application that already works, out of the box. npm install -g @angular/cli  -g is a flag which installs angular/cli globally in o

Javascipt Interview Question and Answers - 2

In this article, I am going to take 3 question which are frequently asked in Javascript interviews. These questions are somewhat linked with the concept of reversing string. These are asked to check the logical ability and approach. “ Give someone a program, you frustrate them for a day; teach them how to program, you frustrate them for a lifetime. ” Let start with an easy one Q1. Write a program to reverse the string/word.e.g. if input is 'india', the output should be 'aidni'. Condition is, the solution should be with minimal lines of code. So pause for a moment, think about it and them reach out to the solution. To solve this problem, we need to think, how we can reverse the string, do we have any in built function to reverse a string? Or should we loop through each character of string from last and assign to new string? Or any other approach? We don't have any built-in function like reverse for a string but we do have for array, Can we make a string

Javascript Interview Question and Answer - 1

In this article we are going to discuss 2 javascript interview question and their answers.These questions are highly asked in the Javascript interviews. Disclaimer: - You might have different solution working with all test cases for both the problems. So lets discuss the first question.This question is to test your ability in terms javascript object knowledge and your logical thinking. Q1. You have been given a nested object "Person" which has one of the property as "password".Write a generic function to change the value of all the "password" property to "*****". Object is nested and may contain other object with property "password" and so on. Here is a sample object. var Person = {      name: 'Alex',      age : 40,     password : 'abc',     x : {        name : 'Chris',        password: 'abcd',        y : {           name : 'Mike',           password: 'abcd',          },      }

What is hoisting in Javascript

One of the most important and little confusing topic in Javascript is Hoisting. The literal meaning of the word ' hoisting ' is ' raising up ' and its meaning w.r.t Javascript also remains the same. Before the execution of Javascript program/file/function, it is parsed by the Javascript engine(e.g. V8 engine in Chrome). During this parsing phase, all the declaration(variable and function) gets hoisted. This mean the memory is allocatted for these variables and functions during the parsing phase. Basically its a default behaviour of Javascript engine to move all the declaration to the top of that function scope in which it is defined . Variable hoisting Since variable declaration are moved on top of the function scope, we don't get error(but undefined value) when we try to fetch a variable before its declaration. e.g. 1.   console.log(num);    // undefined 2.   var num = 5; Here, num defined in line 2 but because of hoisting its declaration is moved on to

Difference between undefined and null

Today we are gonna talk about the famous question in Javascript which all of us must have faced in interview. The difference between undefined and null . But before we talk about their differences, lets first think, why are we comparing undefined and null and why not undefined and string or string and null? So there must be something common,in general, between undefined and null. And that commonality is both of have them represents NO VALUE for the variable. You can also call this NO VALUE as void or empty. So basically when we say as variable is undefined or null that mean the value doesn't exist for the variable. Lets see how is it so. 1.   undefined == null     // true, so the value is same Now lets check each one of them.First lets talk about undefined. When do we get this undefined? Generally when we declare a variable but doesn't initialise it we get undefined. e.g 1.   var foo; 2.   console.log(foo);   // undefined Also when we try to access a vari

Difference between var, let and const

   var, let and const in Javascript        Variable can be declared in three ways: var let const var  variables are function-wide scoped. Let's understand this with an example,    if(true){           var x = 5;    // variable declared with "var"          console.log(x);  // x will be 5    }  console.log(x); // even here, x will be 5          So in the last example we can see variable "x" is available outside the "if" block, that means the scope of variable with "var" is not block scope. let see with another example, 1.  function helloWorld(){       var x = 5;        2.     function abc(){              3.        console.log(x);      // even x will be 5        4.     }        5.     abc();        6.     console.log(x);  // x will be 5 7. } 8. helloWorld();              So in this example, we can see the variable "x" is available throughout the function in which it is declared. One of th