Some JavaScript Questions…

Shaikh Jamil AL-Razi
4 min readMay 8, 2021

1. What is javascript, key features of Javascript?

JavaScript is a high-level programming language that is used widely used in the modern web. It is a client-side as well as a server-side language. server-side how you say by using Node.js which is JavaScript run time for the server.
It has a vast amount of features like dynamic programming, prototype-based object orientation, and first-class functions.

2. What is DOM (Document Object Model)?

When a web page is loaded the browser creates DOM (Document Object Model) of the page. With this DOM JavaScript gets all the power it needs to create dynamic HTML. The DOM defines a standard for accessing documents.

3. How to use the JavaScript callback function?

A callback function is a function that is passed into another function as an argument. the function is called inside another function for completing an action. A code example is given -

function callbackFunction(name) {
console.log(‘Hello ‘ + name);
}

function outerFunction(callback) {
let name = “jamil”;
callback(name);
}

outerFunction(callbackFunction); // Hello jamil

4. What is Truthy and Falsy values in JavaScript?

In JavaScript, a truthy value is a value that is true when encountered in a boolean context. All values are truthy unless they are defined as falsy value. some example of falsy value is given -

console.log(null); // null
console.log(undefined);// undefined
console.log(false); // false
console.log(NaN); // NaN
console.log(“”); // ‘’
console.log(0); // 0

5. What is an API, the purpose of API, GET, POST?

An API is a set of functions that allows applications to access data from external servers. it delivers a user response to the system and sends the system’s response back to the user.
The purpose of API is to send and receive information. GET is an API basic method. It gathers information from the server. POST method in API means creates new data.

6. Use of Bind, Call and Apply in JavaScript?

In JavaScript Bind return a new function allowing you to pass any number of arguments.

let person1 = {firstName: ‘John’, lastName: ‘Rodson’};
let person2 = {firstName: ‘Jimmy’, lastName: ‘Baily’};

function invite(greeting1, greeting2) {
console.log(greeting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName+ ‘, ‘+ greeting2);
}

let invitePerson1 = invite.bind(person1);
let invitePerson2 = invite.bind(person2);
invitePerson1(‘Hello’, ‘How are you?’); // Hello John Rodson, How are you?
invitePerson2(‘Hello’, ‘How are you?’); // Hello Jimmy Baily, How are you?

In JavaScript, the call method calls a function with a given this and arguments are given one by one comma-separated.

let person1 = {firstName: ‘John’, lastName: ‘Rodson’};
var person2 = {firstName: ‘Jimmy’, lastName: ‘Baily’};

function invite(greeting1, greeting2) {
console.log(greeting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName+ ‘, ‘+ greeting2);
}

invite.call(person1, ‘Hello’, ‘How are you?’); // Hello John Rodson, How are you?
invite.call(person2, ‘Hello’, ‘How are you?’); // Hello Jimmy Baily, How are you?

Like call method in apply method calls a function with a given this but arguments are given in an Array.

let person1 = {firstName: ‘John’, lastName: ‘Rodson’};
let person2 = {firstName: ‘Jimmy’, lastName: ‘Baily’};

function invite(greeting1, greeting2) {
console.log(greeting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName+ ‘, ‘+ greeting2);
}

invite.apply(person1, [‘Hello’, ‘How are you?’]); // Hello John Rodson, How are you?
invite.apply(person2, [‘Hello’, ‘How are you?’]); // Hello Jimmy Baily, How are you?

7. Double equal (==) vs triple equal (===) in JavaScript

For Checking conditions in JavaScipt we put double equal or triple between two values, it will return boolean. In double equal, it will not check the type of values but in triple equal, it will check the type and give desired results. example given -

console.log(0 == false) // true
console.log(0 === false) // false
console.log(1 == “1”) // true
console.log(1 === “1”) // false
console.log(null == undefined) // true
console.log(null === undefined) // false
console.log(‘0’ == false) // true
console.log(‘0’ === false) // false
console.log([]==[]) //false, refer different objects in memory
console.log([]===[]) //false, refer different objects in memory
console.log({}=={}) //false, refer different objects in memory
console.log({}==={}) //false, refer different objects in memory

8. What is closure in JavaScript?

A closure is the combination of a function. It is an inner function that has access to the outer function’s variables. The closure has three scope chains

1. Own scope where variables defined between its curly brackets
2. Outer function’s variables
3. Global variables

An example of closure given -

function Welcome(name){
return function(message){
console.log(message+’ ‘+name);
};
}
let myFunction = Welcome(‘John’);
myFunction(‘Welcome ‘); //Output: Welcome John
myFunction(‘Hello Mr.’); //output: Hello Mr.John

9. What is Scope in JavaScript?

The 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.

10 What is the Block scope in JavaScript?

Normally scopes are the accessibility of variables, functions, and objects in some particular part of your code during runtime. let and const in JavaScript are block-scoped they can not be accessed out of their blocked scope. On the other hand, var gets hoisted and can be accessed. let and const are block-level scop var is function-level scope.

--

--