Skip to main content

Posts

Showing posts with the label Basic Level

Javascript Hoisting Interview Question and Answers - 5

In this post,I have tried to cover all the types of possible interview questions and answers on hoisting. If you are not familiar with the hoisting, I would highly recommend you to go to my post on hoisting. Once you are comfortable with the topic, you can attempt these questions. Understanding the type of question will help you to answer. I have provided the hint after each question. All the answers are given at the end of the post.
//Question 1 console.log('bar:', bar) bar = 15 var foo = 1 console.log(foo, bar) var bar Hint: Basics of hoisting //Question 2 var foo = 5 console.log('foo:', foo) var foo; var bar = 10; var bar; console.log('bar:', bar) var baz = 10 var baz = 12 console.log('baz:', baz) Hint: Basics of hoisting //Question 3 function foo() { function bar() { return 5 } return bar() function bar() { return 10 } } console.log(foo()); Hint: Think what happens if two functions wit…

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: 27name: "Alex"__proto__: Object We can also create the same person object differentlylet person = new Object();
person.name = "Alex";
person.age = 27;
console.log(person);
output:
{name: "Alex", age: 27} Generally, property names are kept as string but it can be a string or a number.e.g.
let person = { name : "A…

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 declaration:
const callMe = () => {