JavaScript topics for interview preparation
--
React Hook: ‘Hooks are functions that let you “hook into” React state and lifecycle features from function component. They do not work within a class. They let you use React without a class.’ — from React official blog post.
Hooks avoid the complexity of classes and higher-order components altogether.
Closure: is the combination of a function and the lexical scope within which the particular function is declared.
function start() {
var topic = ‘closure’; // topic is a local variable created by init
function display () { // display() is the inner function, a closure
alert(topic); // use variable declared in the parent function
}
display();
}
start();
Accessibility: design and creation of websites that can be used by everyone. Which allows assistive technology to interpret web pages.
A number of tools exist that can run accessibility audits on web pages:
- Web Accessibility Evaluation Tool
- NVDA in firefox
- JAWX in Internet Explorer
- ChromeVox in Google Chrome
Truthy and Falsy: each value has an inherent boolean value, generally know as either truthy or falsy.
Values that are always Falsy:
- False
- 0 (zero)
- ‘’ or “”(empty string)
- Null
- undefined
- Nan
Everything else is Truthy:
- ‘0’
- ‘false’
- [ ]
- { }
- function ( ) { }
undefined vs null:
undefined: anything that is not defined.
let name;
console.log(name); // output: undefined
null: is an assignment value, we can assign it to a variable.
let name = null;
console.log(name); // output: null
Scope: is the accessibility of variables, functions, and objects in some particular part of our code during runtime. It provides the same level of security to our code.
Types of Scope:
- Local Scope: variable defines inside a function.
- Global Scope: variable defines outside a function.
JavaScript coding proble:
Find the largest element of an Array:
const array1 = [1, 2, 3, 4];
console.log(Math.max(…array1));
// output: 4
Sume of all numbers in an Array:
arrSum = function(arr){
return arr.reduce(function(a,b){
return a + b
}, 0);
}
Remove duplicates:
let arr = [1, 2, 3, 2, 4, 5];
function removeDuplicates(items) {
return items.filter((value, index) => items.indexof(value) === index);
}
console.log(removeDuplicates(arr));
// [1, 2, 3, 4, 5]
Asynchronous in JavaScript: In asynchronous programs, we can have two lines of code, where the first-line schedules some task to be run in the future, but second-line runs before that task completes.
setTimeout(function() {
console.log(‘asynchronous message’);
}, 1000);
console.log(‘synchronous message’);
JavaScript Methods: are actions that can be performed on objects. A method is a property that contains a function definition.
Object property: is a variable that is attached to the object.
Difference between object method and object property: we can think of object properties like object variable of the object and method like function. For example, a string has .length property it’s like a variable and here .toLowerCase() method looks like a function.
Check a number is a Prime number or not:
// return true if num is prime
function isPrime(num)
{
// if num is less than 2 => is not prime
if ( num < 2 ) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) return false;
}
return true;
}