10 Questions in JavaScript

1. Define is truth and false values in JavaScript(what is it?)

Because of a certain value and sometimes even putting an empty string can give true as a result when applying condition. But when it gives true and when it gives false.

If the value of a variable is set 0, JavaScript considers it as a false value, and the return condition is false. When the value of a variable is set to any number other than 0 it consider the condition as true.

Similarly, if the value of a variable is a string it will return true other than (‘’) empty string. But if it is set with whitespace inside a string, it’s not an empty string anymore and would return true as a condition.

If the value is set to null, it returns false and when you don’t provide a value it returns undefined as the variable's value is not set which also considered as falsa as a condition.

Here’s some example.

let place = ‘’;

let place = ‘ ‘;

let place = null;

let place = false;

let number = false;

let number = NaN;

let number;

let age = 24;

let age = ‘24/false’;

let ages = [];

2. What’s the difference between null and undefined

Null is an assignment value. It means you put intentional absence of value of a variable. To get null which is non-existent value you must set a variables value to null.

let place = null;

But for undefined you don’t need to assign a value to a variable. Meaning is so simple, a variable is declared but the value is not initialized or assigned.

let number;

All the non-existent properties of an object are considered undefined.

let object = {};

Both null and undefined are falsy values. A null is an object whereas undefined is a type itself. Null represents the absence of value of a variable on the other hand undefined represents the absence of variable itself. Null converts to 0 when performing primitive operations while undefined converts to NaN(not a number). That’s all.

3. What’s the difference between Double equal (= =) and Triple equal (= = =)?

Triple equal denotes to strict equality between something which needs to be the same in both values and types. That means if two things have the same value and type then triple equal will return true if they are compared. Value same but not type, then it will be false.

100 === ‘100’

Like the above example value 100 is the same but one is just a number and the other is string type. So it is not comparable with triple equals.

‘place’ === ‘place’

Above example, both the string type and value are the same. But if the value is not the same it will be false.

‘place’===’rajshahi’

For the above type string same but not value. Here’s the interesting part, comparing with

= = gives some loose in this case.

Like

100 == ‘100’

Value same not type but still it will be considered as true or correct things to compare between two things. Double equals compare similarity after both comparable items are converted to a common type.

For Boolean like

false === 0 return false

false == 0 return true

So this is the basic difference.

4. What’s the difference between bind(), call(), and apply()

bind ()

var useful = {

firstname: ‘Some’,

lastname: ‘thing’,

getName: function () {

var fullName = this.firstname + this.lastname;

return fullName;

}

};

var myName = function () {

console.log(this.getName() + ‘ ‘ + ‘i needed it’);

}

var showName = myName.bind(useful);

showName()

Here output is ‘Something i needed it’

Look the myName function was not in the ‘useful’ object. But because of bind myName with useful, this treats as its variable. That’s why after creating an object instance myName calls the logShowName function. And like normal function arguments, can also be passed as parameters in the myName() function.

call() and apply()

call() method calls a function based on this value and the arguments that are passed individually. But the main difference from bind is that it does not copy the main function that is called. And the most interesting part is this keyword can be referenced when calling the function explicitly. apply() is close to the same except that call() method expects all parameters to be passed individually and apply() expects an array of parameters.

5. What is javascript?

JavaScript is a scripting language that enables showing dynamic content, images, information, dynamic interaction on a webpage or website. One thing why JavaScript is used is that it enables you to load or change HTML, CSS codes to update user interfaces via Document Object Model (DOM) API. Now, what is API? API stands for Application Programming Interface. JavaScript enables you to use these ready-made sets of building block codes for specific program-based tasks which in turn goes to interface display which would be difficult or in some way impossible to implement. When you load a webpage it runs some codes in browser environment tabs, but if JavaScript loads before HTML and CSS before the correct time errors can occur. JavaScript is considered a lightweight interpreted programming language which uses just-in-time-compiling techniques to improve quality and better performance. JavaScript receives the code in text form and runs from the script. JavaScript also gives you the opportunity to run server-side code or codes which run on the server and then the browser loads it and displays it. The popular JavaScript server-side language is NodeJS. The html page appears as the order of codes, so any manipulation of elements could result in occurring. That’s why asynchronous JavaScript run and load is very much needed.

6. What is the callback function?

A callback is a function passed as an argument in another function. JavaScript function executes based on the sequence of their call, not the sequence of defining. So callback is a function that is to be executed or run after finishing another function. Sometimes functions can wait to execute or don’t run immediately after calling. For using some API, we need to wait for response, and then the function executes. So if there is any need for specific requirements where a function will run only after getting a response from the user or event-listening handler, this is where a callback function is passed on that function as an argument and tells it to run after the timeout. But sometimes because of this delay functions won’t execute as the order we want. The callback is a way to confirm that some codes won’t execute until another function is finished executing.

function getFood(type, callback) {

console.log(`get ${type} from market on your way back`);

callback();

}

function cookFood() {

console.log(“doesn’t need to be cooked”)

}

getFood(‘fruit’, cookFood);

Above example, the cookFood function is passed as an argument when calling getFood() function. So the function getFood() executes before cookFood() function no matter in what order you define the function.

7. How Recursion works?

When a function calls itself that is called a recursive function. Programming tasks can be solved using a loop instantly or can be splinted into several layers of tasks. During solving problems a function can call several other functions. And at that time in any of the parts if the function calls itself that is when recursion will works. A simple example with code can be understandable

function multipleNumber(x, n) {

if (n == 0) {

return 0

}

else if (n == 1) {

return x

}

else {

return x + multipleNumber(x, n — 1);

}

}

console.log(multipleNumber(2, 0)); return 0;

console.log(multipleNumber(2, 1)); return 2

console.log(multipleNumber(2, 8)); return 16

Look up the above example, a simple addition is broken down in many steps. And during executing the function multipleNumber() calls itself repeatedly to add 2 eight times and give the result. This is called recursion of function. When a function executes its execution-related data is stored in its context stack. For the above function n==0 and n==1 is false so it jumps in else line where recursion is happening. Here first of all function executes nultipleNumber(2, 8); then it is stored in the context stack. After that function calls itself for (2, 7) respectively and goes on till n==1. And after executing another task like addition is resumed and final results show the total addition of 2 eight times.

This is how recursion works.

8. What is Scope?

The scope is what determines the accessibility and visibility of a variable in the area of codes.

There are many scope types.

Ø Global Scope

Ø Local Scope

Ø Function Scope

What is the global scope? : Well the area outside a function is considered a global area. And this is the global scope where a declared variable is accessible from any function.

Local scope: Variables that are declared inside a function become local scope to that function. All the function has its local area to define local which is only accessible in that function. Local scope are two types

Ø Function Scope

Ø Block Scope

Whenever a variable is declared using var keywords inside a function it is accessible in that function. Which is Function scope.

Block scope is that when you declare a variable using let, const keywords inside a condition in a function is called block scope. And the variable is only accessible in that condition under that function not outside the function.

9. What is Encapsulation?

Encapsulation is the bundling of data. This means some sets of data are stored in a capsule and the data accessibility is restricted from outside. To change or access encapsulated data methods are need to call on the object and apply the method to change the state. After that object responds by updating its state.

Encapsulation is necessary. Because the problem occurs with a shared mutable state. If an input of function or instruction depends on the output of other instruction then based on their calling sequence the result varies.

This is where encapsulation is applied. It prevents many bugs and avoids shared mutable state collision and composition.

10. What is Closure?

A closure is the combination of a function bundled together with references to its surrounding state (the lexical environment). So closure mostly works on lexical scope or environment. When you create a function you also start a closure. Closure gives access to an outer function scope from inside or inner function scope.

function simple() {

var greeting = ‘Hello Moto’;

function displayGreeting() {

console.log(greeting);

var name = ‘Motorola’

}

displayGreeting()

}

simple();

Look up the above example. simple() is a function and variable greeting is declared in the function scope. displayGreeting() is inner function. But because of function scope and closure from inner function variable greeting is accessible. But the variable name is not accessible from outside the displayGreeting() function, because it has its own closure.

So basically a closure is a combination of function and the lexical environment within which the function was declared.

I am an engineer. Currently I am studying Material Science & Engineering .And I am also a Front End Web Developer.