Advanced javascript: questions and answers + some basic problems and their solution with javascript

Double Equals (==) vs Triple Equals (===):

Double Equals (==) checks for value equality only. It converts the types of the variables to match each other.

On the other hand, Triple Equals (===) will verify whether the variables being compared have both the same value and the same type.

Recursion vs iterative: The Recursion and Iteration both repeatedly execute the set of instructions. Recursion is when a statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes until the controlling condition becomes false. The primary difference between recursion and iteration is that recursion is a process, always applied to a function and iteration is applied to the set of instructions which we want to get repeatedly executed.

API: An API (Application Programming Interface) is a set of functions that allows applications to access data and interact with external software components, operating systems, or microservices. To simplify, an API delivers a user response to a system and sends the system’s response back to a user.

DOM: The Document Object Model (DOM) is a programming API for HTML and XML documents. It defines the logical structure of documents and the way a document is accessed and manipulated. With the Document Object Model, programmers can create and build documents, navigate their structure, and add, modify, or delete elements and content.

This: In JavaScript, the this keyword refers to the object that is currently executing the code. By default, this refers to the global object.In a function, when not in strict mode, this refers to the global object. In strict mode, this is undefined. In an arrow function, this retains the value of the enclosing lexical context’s this. In an object method, this refers to the object the method was called on. In a constructor call, this is bound to the new object being constructed. In an event handler, this is bound to the element on which the listener is placed.

Window, global variable, global scope: The window object is supported by all browsers. It represents the browser’s window. All global JavaScript objects, functions, and variables automatically become members of the window object. Global variables are properties of the window object. Global functions are methods of the window object. Even the document object (of the HTML DOM) is a property of the window object

A variable declared at the top of a program or outside of a function is considered a global scope variable.

In a programming environment, the global scope is the scope that contains, and is visible in, all other scopes. In client-side JavaScript, the global scope is generally the web page inside which all the code is being executed.

Remove duplicate from an array: ES6 has a native object Set to store unique values. To get an array with unique values you could do now this:

const myArray = [‘a’, 1, ‘a’, 2, ‘1’];

let unique = […new Set(myArray)];

console.log(unique); // unique is [‘a’, 1, 2, ‘1’]

The constructor of Set takes an iterable object, like Array, and the spread operator (…) transform the set back into an Array.

Reverse a string: A string is an array of characters, so you can use the reverse function on the array to reverse it and then return it:

function reverseString(str) {

return str.split(‘’).reverse().join(‘’);

}

create Fibonacci series in a recursive way:

function fibonacci(n) {

return n < 1 ? 0

: n <= 2 ? 1

: fibonacci(n — 1) + fibonacci(n — 2);

}

console.log(fibonacci(4));

Check prime number:

function isPrime(num){

if(num==2) return true;

for(i=2;i<Math.sqrt(num);i++) {

if(num % i==0) return false;

}

return true;

}