JavaScript Flashcards

1
Q

List ways to create an Object

A
  1. Object constructor :
    var obj = new Object()
  2. Object’s create method
    var obj = Object.create(null)
  3. Object literal syntax
    var obj = {}
  4. Function constructor
    function Person(name) {}
    var obj = new Person(“pat”)
  5. Function constructor with prototype :
    function Person() {}
    Person.prototype.name = “Sudheer”;
    var object = new Person();
  6. ES6 Class syntax
    class Person {
    constructor(name) {
    this.name = name;
    }
    }

var object = new Person(“Sudheer”);
7. Singleton pattern
var object = new (function () {
this.name = “Sudheer”;
})();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is prototype chaining

A

A mechanism in JS that allows objects to inherits from another object
เป็นกรไรของ JS ที่ให้ object สามารถ สืบทอดต่อจาก object ตัวอื่น

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between call(), apply() and bind()

A

Call()
invokes function with a given THIS value and arguments
Apply()
Similar to Call() just with an array
Bind()
Returns new function and allow you to pass any number of arguments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is JSON and its common operations

A

JSON is a text-based format following JS object syntax.
Common op :
Parse : convert string to native object
stringify : convert native object to string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the purpose of slice and splice methods?

A

Slice :
Selects elements in a given array and return a new array with the selected element.
Splice:
Used either to add/ remove items to/from an array and then returns the removed item.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what’s the differences between Object and Map?

A

Object:
- key are strings and symbol and are unordered
- Have to manually calculate the size
- Can’t directly iterate over Object without getting its keys in some way and iterate over them
Map
- Keys can be any value including functions, object and primitives
- Keys in map is ordered
- Can easily get size of map with size()
- Iterable directly
- Perform better in scenarios liek frequent addition and removal of key pairs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are arrow functions?

A

A shorter syntax for function expression and doesn’t have its own this , arguments, super, or new.target.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is first class functions ?

A

When functions in that language are treated like other variables (like passing to other function or returned by a function)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is first order functions ?

A

Function that doesn’t accept another function as argument and doesn’t returns a function as value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is higher order functions ?

A

Functions that accept another function as an argument or returns a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is unary functions?

A

A function that accepts exactly one argument.
Const unaryFunc = (a) => {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is currying functions ?

A

The process of taking a function with multiple arguments and turning it into a sequence of functions each with only one argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is pure function ?

A

Function that the return value is determinded by its argument without any side effects (or any changes from outer code)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the difference between let, const and var?

A

let and const are block scoped
variable with let can be reassigned while const cannot
var is a global scoped variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you redeclare variables in switch block without and error

A

create a nested block inside a case clause and create a new block scoped lexical environment.
let counter = 1;
switch (x) {
case 0: {
let name;
break;
}
case 1: {
let name; // No SyntaxError for redeclaration.
break;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Temporal Dead Zone?

A

is a behavior in JavaScript that occurs when accessing a let or const variable before its declaration (within its scope) causes a ReferenceError. The time span when that happens, between the creation of a variable’s binding and its declaration, is called the temporal dead zone.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is IIFFE

A

is a JavaScript function that runs as soon as it is defined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is Hoisting ?

A

mechanism where variables, function declarations and classes are moved to the top of their scope before code execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are classes in ES6?

A

Javascript classes are primarily syntactic sugar over JavaScript’s existing prototype-based inheritance

20
Q

What are closures?

A

The ability of a function to access data from its lexical scope even though the lexical scope is destroyed

21
Q

What are modules ?

A

Modules refer to small units of independent, reusable code and also act as the foundation of many JavaScript design patterns. Most of the JavaScript modules export an object literal, a function, or a constructor

22
Q

Why do you need modules ?

A

Maintainability
Reusability
Namespacing

23
Q

What is a scope in JS?

A

Scope is the accessibility of variables, functions, and objects in some particular part of your code during runtime. In other words, scope determines the visibility of variables and other resources in areas of your code.

24
Q

What is web storage?

A

Web storage is an API that provides a mechanism by which browsers can store key/value pairs locally within the user’s browser

25
Q

What is cookie?

A

A cookie is a piece of data that is stored on your computer to be accessed by your browser. Cookies are saved as key/value pairs

26
Q

What are the options in a cookie?

A
  1. By default, the cookie is deleted when the browser is closed but you can change this behavior by setting expiry date (in UTC time).
  2. By default, the cookie belongs to a current page. But you can tell the browser what path the cookie belongs to using a path parameter.
27
Q

what are the differences between cookie, local storage and session storage?

A

Cookies: Cookies are small text files that are stored in the browser’s memory or on the user’s hard drive. They are used to store user-specific information such as login credentials, shopping cart items, and preferences. Cookies can be set to expire after a certain time or when the user closes the browser. They are typically sent to the server with each request, allowing the server to maintain state between requests.

Local Storage: Local storage is a feature in modern web browsers that allows web applications to store data locally within the user’s browser. Local storage provides a way to store data without the data being sent to the server with each request. Local storage data is persistent, which means it remains available even after the browser is closed and reopened.

Session Storage: Session storage is similar to local storage, but the data is only stored for the duration of the session. A session is a period of time when a user is actively engaged with a web application, typically starting when the user logs in and ending when the user logs out or closes the browser. Session storage data is stored in memory, and like local storage data, it is available to the web application without being sent to the server with each request.

28
Q

What is a promise?

A

A promise is an object that may produce a single value some time in the future with either a resolved value or a reason that it’s not resolved(

29
Q

Why do you need a promise ?

A

Promises are used to handle asynchronous operations. They provide an alternative approach for callbacks by reducing the callback hell and writing the cleaner code.

30
Q

What are the three states of promise?

A

It will be in one of the 3 possible states: fulfilled, rejected, or pending.

31
Q

what is a callback function?

A

A callback function is a function passed into another function as an argument. This function is invoked inside the outer function to complete an action.

32
Q

why do you need call back function

A

The callbacks are needed because javascript is an event driven language. That means instead of waiting for a response javascript will keep executing while listening for other events.

33
Q

what is a callback hell?

A

Callback Hell is an anti-pattern with multiple nested callbacks which makes code hard to read and debug when dealing with asynchronous logic

async1(function(){
async2(function(){
async3(function(){
async4(function(){
….
});
});
});
});

34
Q

what are the main rules of promise?

A

A promise must follow a specific set of rules:

  1. A promise is an object that supplies a standard-compliant .then() method
  2. A pending promise may transition into either fulfilled or rejected state
  3. A fulfilled or rejected promise is settled and it must not transition into any other state.
  4. Once a promise is settled, the value must not change.
35
Q

what is promise chaining?

A

The process of executing a sequence of asynchronous tasks one after another using promises

36
Q

what is promise.all ?

A

Promise.all is a promise that takes an array of promises as an input (an iterable), and it gets resolved when all the promises get resolved or any one of them gets rejected

37
Q

what is the purpose of the race method in promise?

A

will return the promise instance which is firstly resolved or rejected.

var promise1 = new Promise(function (resolve, reject) {
setTimeout(resolve, 500, “one”);
});
var promise2 = new Promise(function (resolve, reject) {
setTimeout(resolve, 100, “two”);
});

Promise.race([promise1, promise2]).then(function (value) {
console.log(value); // “two” // Both promises will resolve, but promise2 is faster
});

38
Q

What is a strict mode ?

A

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a “strict” operating context. This way it prevents certain actions from being taken and throws more exceptions

39
Q

how do you declare strict mode ?

A

The strict mode is declared by adding “use strict”; to the beginning of a script or a function

40
Q

What is typeof operator?

A

You can use the JavaScript typeof operator to find the type of a JavaScript variable. It returns the type of a variable or an expression.

41
Q

what is the differences between null & undefined?

A

Undefined
indicates that a variable has not been assigned a value, or declared but not initialized at all
Null
null represents the intentional absence of any object value. It is one of JavaScript’s primitive values. The type of null value is object

42
Q

What is the differences between undelcared and undefined ?

A

Undefined
is a value that a declared variable can have when it has not been assigned a value. If you access it, it’ll just return undefined.
Undeclared
refers to a variable that has not been declared at all. If you try to access an undeclared variable, you will get a reference error

43
Q

what is an event flow?

A

Is the order in which event is received on the web page. When you click an element that is nested in various other elements, before your click actually reaches its destination, or target element, it must trigger the click event for each of its parent elements first, starting at the top with the global window object

44
Q

what is an event delegation?

A
45
Q

what is event bubbling and capturing ?

A

Bubbling
type of event propagation where the event first triggers on the innermost target element, and then successively triggers on the ancestors (parents) of the target element in the same nesting hierarchy till it reaches the outermost DOM element.
Capturing
type of event propagation where the event is first captured by the outermost element, and then successively triggers on the descendants (children) of the target element in the same nesting hierarchy till it reaches the innermost DOM element.