interface and type
Function overlaoading
OOPS Concept
Anonymous
undefined error
decode and encode URI
escape and un escape function'
unshift position in Java script
Screen Obects in Java Script
Event handlers in Java Script
Event bubbling
Redux v/s Flux
Components of Redux
What is redux
Role of reducer
Fragment - what is the signifance
Higher order Compoenent
Pure compoenent
Mounting, Unmount, Constructor
Render method on React
When it is invoked .. what result will it give - render
state and stateless components
Redux - what is it ?
Flux
Higher Order components
Control and Uncontrolled compoenents - Prop and States
Lifecycle
Java Script ----Not Advised to use inner HTML - Why ?
Unshift Method - Explain
Screen Objects - Explain
Event Bubbling in Javascript
Method Overriding
-generics in type script
- write code to print prime number 2- 20
***************************************************************
L2
class components and functional components and hooks
How it helps - to use above which is better
Paradigm shift - Difference between OOP v/s Procedure approach - What is intentions of using hooks, what is difference
Styling - Style component as a pattern
how do you style react component.
Style vs/ normal component - difference
React 15 - what is purpose of context
React 16 - context api and state management
what is prop drilling ?
Functional componet - 10 props
1st child - 5 props and rest 5 props will still pass - not used , will it effect rendering
Few more L2 questions
Why react has introduced hooks and what impact new hooks related features are making ?
Why we should not doing prop drilling and use react' context API?
Different between functional components and classs components?
How react diffing algo works?
And one practical example of search input and dropdown in react
******************************************************************************
class components and functional components and hooks
How it helps - to use above which is
better
Paradigm shift - Difference between OOP v/s Procedure approach - What is intentions of using hooks, what is difference
Styling - Style component as a pattern
how do you style react component.
Style vs/ normal component - difference
React 15 - what is purpose of context
React 16 - context api and state management
what is prop drilling ?
Functional componet - 10 props
*******************************************************************************************
One more inputDesign a simple page with a textbook where there are some autocomplete suggestions, with some styling **************************************************************************************************
React state management
2.How it works in react js
3.Why we use React Hooks
4.Difference between React hook cycle of Class vs Functional components
5.How the states work in class as well functional components6.Design Search box with auto suggestion box using json data --- Online Test
Autocomplete allows the browser to predict the value. When a user starts to type in a field, the browser should display options to fill in the field, based on earlier typed values.
AutoFocus The autofocus
attribute is a boolean attribute.
When present, it specifies that an <input>
element should automatically get focus when the page loads.
required The required
attribute is a boolean attribute.
When present, it specifies that an input field must be filled out before submitting the form.
URL — Uniform Resource Locator
HTML URL Encoding Reference
URL Encoding (Percent Encoding)
URL encoding converts characters into a format that can be transmitted over the Internet.
URLs can only be sent over the Internet using the ASCII character-set.
Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.
URL encoding replaces unsafe ASCII characters with a “%” followed by two hexadecimal digits.
URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.
URL Encoding Functions
In JavaScript, PHP, and ASP there are functions that can be used to URL encode a string.
In JavaScript you can use the encodeURIComponent() function.
The decodeURIComponent() function decodes a URI component.
Note: The JavaScript function encodes space as %20.
ASCII Encoding Reference
Your browser will encode input, according to the character-set used in your page.
The default character-set in HTML5 is UTF-8.
Function Overloading
Unlike the other programming languages JavaScript Does not support Function Overloading. but we can implement the function Overloading on our own, which is pretty much complex when it comes to more number and more type of arguments.
In JavaScript if two functions are defined with same name, then the last defined function will overwrite the former function.
// Creating a class "foo"
class foo {// Creating an overloable method/function.
overloadableFunction() {// Define three overloaded functions
var function1 = function (arg1) {
console.log("Function1 called with"
+ " arguments : " + arg1);
return arg1;
};var function2 = function (arg1, arg2) {
console.log("Function2 called with"
+ " arguments : " + arg1
+ " and " + arg2);
return arg1 + arg2;
};var function3 = function (arg1) {
var concatinated__arguments = " ", temp = " "// Concatinating all the arguments
// and storing them into a string
for (var i = 0; i < arg1.length; i++) {
concatinated__arguments =
concatinated__arguments + arg1[i]
}/* Just ignore this loop and temp variable,
we are using this loop to concatinate
arguments with a space between them */
for (var i = 0; i < arg1.length; i++) {
temp = temp + " " + arg1[i]
}console.log("Function3 called with this"
+ " array as an argument : [" + temp + "]");
console.log("Output of log is : ")// Returns concatinated argument string
return concatinated__arguments;
};/* Here with the help of the length of the
arguments and the type of the argument
passed ( in this case an Array ) we
determine which function to be executed */
if (arguments.length === 1
&& Array.isArray(arguments[0])) {
return function3(arguments[0]);
} else if (arguments.length === 2) {
return function2(arguments[0], arguments[1]);
} else if (arguments.length === 1
&& !Array.isArray(arguments[0])) {
return function1(arguments[0]);
}
}
}// Driver Code// Instantiate an object of the "foo" class
var object = new foo();// Call the overloaded functions using the
// function overloadableFunction(...)
// We are passing 1 argument so executes function1
console.log(object.overloadableFunction("Geeks"));// We are passing two arguments so executes function2
console.log(object.overloadableFunction("Geeks", "for"));// We are passing an array so executes function3
console.log(object.overloadableFunction(
["Geeks", "for", "Geeks"]));
Output:
Function1 called with arguments : Geeks
GeeksFunction2 called with arguments : Geeks and for
GeeksforFunction3 called with this array as an argument : [ Geeks for Geeks]
GeeksforGeeksOutput of log is :
GeeksforGeeks
Screen Object
The screen object contains information about the visitor’s screen.
availHeight Returns the height of the screen (excluding the Windows Taskbar).
availWidth Returns the width of the screen (excluding the Windows Taskbar).
height Returns the total height of the screen.
width Returns the total width of the screen.
colorDepth Returns the bit depth of the color palette for displaying images.
pixelDepth Returns the color resolution (in bits per pixel) of the screen.
splice function:
The splice() method adds/removes items to/from an array, and returns the removed item(s).
Note: This method changes the original array.
arr.splice(index, 0, itemsToadd);
0: items to delete
will insert itemsToadd
into arr
at the specified index
.
var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.splice(2, 0, "Lemon", "Kiwi");
fruits.splice(2, 1, "Lemon", "Kiwi");#Output:
Banana,Orange,Lemon,Kiwi,Apple,Mango
Banana,Orange,Lemon,Kiwi,Mango
slice() method
The slice() method returns the selected elements in an array, as a new array object.
The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1, 3);#output:
Orange,Lemon
Note: The original array will not be changed.
unshift() Method
The unshift() method adds new items to the beginning of an array, and returns the new length. Note: This method changes the length of an array. Tip: To add new items at the end of an array, use the push() method.
In TypeScript, we have a lot of basic types, such as string, boolean, and number. These are the basic types of TypeScript. You can check the list of all the basic types here. Also, in TypeScript, we have advanced types and in these advanced types, we have something called type aliases. With type aliases, we can create a new name for a type but we don’t define a new type.
Interfaces in JS
There’s no notion of “this class must have these functions” (that is, no interfaces per se), because:
- JavaScript inheritance is based on objects, not classes. That’s not a big deal until you realize:
Types vs. interfaces
The difference between types and interfaces in TypeScript used to be more clear, but with the latest versions of TypeScript, they’re becoming more similar.
Interfaces are basically a way to describe data shapes, for example, an object.
Type is a definition of a type of data, for example, a union, primitive, intersection, tuple, or any other type.
1. Declaration merging
One thing that’s possible to do with interfaces but are not with types is declaration merging. Declaration merging happens when the TypeScript compiler merges two or more interfaces that share the same name into only one declaration.
Let’s imagine that we have two interfaces called Song
, with different properties:
interface Song {
artistName: string;
};interface Song {
songName: string;
};const song: Song = {
artistName: "Freddie",
songName: "The Chain"
};
TypeScript will automatically merge both interfaces declarations into one, so when we use this Song
interface, we’ll have both properties.
Declaration merging does NOT work with types. If we try to create two types with the same names, but with different properties, TypeScript would still throw us an error.
Duplicate identifier Song.
2. Extends and implements
In TypeScript, we can easily extend and implement interfaces. This is not possible with types though.
for more @https://blog.logrocket.com/types-vs-interfaces-in-typescript/
When a subclass extends a class, it allows the subclass to inherit (reuse) and override code defined in the supertype.
When a class implements an interface, it allows an object created from the class to be used in any context that expects a value of the interface.
Intersection
Intersection allows us to combine multiple types into a single one type. To create an intersection type, we have to use the &
keyword:
Unions
Union types allow us to create a new type that can have a value of one or a few more types. To create a union type, we have to use the |
keyword.
Tuples
Tuples are a very helpful concept in TypeScript, it brought to us this new data type that includes two sets of values of different data types.
type Reponse = [string, number]
But, in TypeScript, we can only declare tuples using types and not interfaces. There’s no way we can declare a tuple in TypeScript using an interface, but you still are able to use a tuple inside an interface, like this:
interface Response {
value: [string, number]
}
We can see that we can achieve the same result as using types with interfaces.
interfaces in Typescript
An interface is a syntactical contract that an entity should conform to. In other words, an interface defines the syntax that any entity must adhere to.
Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. It is the responsibility of the deriving class to define the members. It often helps in providing a standard structure that the deriving classes would follow.
Let’s consider an object −
var person = {
FirstName:"Tom",
LastName:"Hanks",
sayHi: ()=>{ return "Hi"}
};
If we consider the signature of the object, it could be −
{
FirstName:string,
LastName:string,
sayHi()=>string
}
To reuse the signature across objects we can define it as an interface.
Declaring Interfaces
The interface keyword is used to declare an interface. Here is the syntax to declare an interface −
Syntax
interface interface_name {
}
Example: Interface and Objects
interface IPerson {
firstName:string,
lastName:string,
sayHi: ()=>string
} var customer:IPerson = {
firstName:"Tom",
lastName:"Hanks",
sayHi: ():string =>{return "Hi there"}
} console.log("Customer Object ")
console.log(customer.firstName)
console.log(customer.lastName)
console.log(customer.sayHi()) var employee:IPerson = {
firstName:"Jim",
lastName:"Blakes",
sayHi: ():string =>{return "Hello!!!"}
}
console.log("Employee Object ")
console.log(employee.firstName);
console.log(employee.lastName);
The example defines an interface. The customer object is of the type IPerson. Hence, it will now be binding on the object to define all properties as specified by the interface.
Another object with following signature, is still considered as IPerson because that object is treated by its size or signature.
How To Use Generics in TypeScript
Introduction
Following the Don’t-Repeat-Yourself (DRY) principle is important when it comes to writing dynamic and reusable code. Using generics can help you achieve this in your TypeScript code.
With generics, you can write dynamic and reusable generic blocks of code. Furthermore, you can apply generics in TypeScript to classes, interfaces, and functions.
In this article, you will integrate generics into your TypeScript code and apply them to functions and classes. You will also learn how to add constraints to generics in TypeScript by using interfaces.
Prerequisites
To successfully complete this tutorial, you will need the following:
- The latest version of TypeScript installed on your machine. This How To Set Up a New TypeScript Project tutorial can help you accomplish this.
- The latest version of
ts-node
installed. This is necessary if you would like to test and run your TypeScript code. This Running TypeScript Scripts With Ease with ts-node tutorial is a great place to start. - Familiarity with writing functions and classes in TypeScript.
Step 1 — Understanding Generics
Sometimes, you may want to repeat the same block of code for different data types. Here’s an example of the same function being used for two different data types:
// for number type
function fun(args: number): number {
return args;
}// for string type
function fun(args: string): string {
return args;
}
Note that, in this example, the same function is being repeated for number
and string
types. Generics can help you to write generalized methods instead of writing and repeating the same block of code, as in the above example.
Alternative: There is a type called any
, which you can use to achieve the same effect as generics in your code. Using the any
type will allow you to opt-out of type-checking. However, any
is NOT type-safe
. This means that using any
can give you an exception.
To see this in practice, apply the any
type to the previous code example:
function fun(args: any): any {
return args;
}
Swapping
number
andstring
types to theany
type makes the function generic. But there’s a catch—using theany
type means that thefun
function can accept any data. As a result, you are losing type safety as well.
Although using the any
type is a way to make your TypeScript code more generic, it may not always be the best option. In the next step, you explore a different option for creating type-safe generics.
useCases of HOC
showing a loader, while a component waits for data
a very frequently needed feature in our app is to show a loader while a component is waiting for data to be passed into it’s props. of course an in-component solution to have a loader can work, it won’t be the most elegant solution. the idea is to create a common HOC, that can track that prop and while it’s empty, can show a loading state.
Over-riding & not Overloading
JavaScript supports overriding, not overloading. When you define multiple functions that have the same name, the last one defined will override all the previously defined ones and every time when you invoke a function, the last defined one will get executed.
There is no real function overloading in JavaScript since it allows to pass any number of parameters of any type. You have to check inside the function how many arguments have been passed and what type they are. The correct answer is THERE IS NO OVERLOADING IN JAVASCRIPT.
function foo(arg1) {
console.log(arg1);
}function foo(arg1, arg2) {
console.log(arg1, arg2);
}// Driver codefoo("Geeks")
The reason for the “undefined” in the output is: In JavaScript if two functions are defined with same name then the last defined function will overwrite the former function.
So in this case the foo(arg1) was overwritten by foo(arg1,arg2), but we only passed one Argument (“Geeks”) to the function. It means that the second argument is undefined, So when we tried to print the second argument, it is printed as “undefined”.
We have seen that function Overloading is not support in JavaScript, but we can implement the function Overloading on our own, which is pretty much complex when it comes to more number and more type of arguments.
for more @ https://www.geeksforgeeks.org/function-overloading-in-javascript/
Why using innerHTML sometimes is NOT recommended
there are some disadvantages to using innerHTML in JavaScript.
The use of innerHTML very slow:
The process of using innerHTML is much slower as its contents as slowly built,
also already parsed contents and elements are also re-parsed which takes time.
- Use innerHTML when you’re setting text inside of an HTML tag like an anchor tag, paragraph tag, span, div, or textarea.
- Use appendChild() If you’re trying to add new DOM elements inside of another DOM element.
In other words, creating a new element and appending it to the DOM tree provides better performance than the innerHTML
.
createElement is more performant
You can new elements to the div
element by creating an element and appending it:
let div = document.querySelector('.container');
let p = document.createElement('p');
p.textContent = 'JS DOM';
div.appendChild(p);
Using DocumentFragment for composing DOM Nodes
Assuming that you have a list of elements and you need in each iteration:
let div = document.querySelector('.container');for (let i = 0; i < 1000; i++) {
let p = document.createElement('p');
p.textContent = `Paragraph ${i}`;
div.appendChild(p);
}Code language: JavaScript (javascript)
This code results in recalculation of styles, painting, and layout every iteration. This is not very efficient.
To overcome this, you typically use a DocumentFragment
to compose DOM nodes and append it to the DOM tree:
let div = document.querySelector('.container');// compose DOM nodes
let fragment = document.createDocumentFragment();
for (let i = 0; i < 1000; i++) {
let p = document.createElement('p');
p.textContent = `Paragraph ${i}`;
fragment.appendChild(p);
}// append the fragment to the DOM tree
div.appendChild(fragment);Code language: JavaScript (javascript)
In this example, we composed the DOM nodes by using the DocumentFragment
object and append the fragment to the active DOM tree once at the end.
A document fragment does not link to the active DOM tree, therefore, it doesn’t incur any performance.
Method Over-riding in TypeScript
- Method Overriding is a concept of Inheritance in Classes where a child class can override a function of a parent class. In this, child class may or may not use the logic of a function defined in parent class.
- We use super keyword to refer to the functions or fields of the immediate parent class.
Below is an example of method overriding USING super. If we use super, the same method of parent class is executed.

Below is the output of above code

Below is an example WITHOUT using super. If we skip super, then that method of parent class is NOR executed. Only the method of child class is executed.

Below is the output of above code

Autocomplete feature in React
more at https://codepen.io/ulanbek/pen/WYyvrL
ABstract Classes
An abstract class must be declared with an abstract keyword. It can have abstract and non-abstract methods. It cannot be instantiated. It can have constructors and static methods also. It can have final methods which will force the subclass not to change the body of the method.
Does a change in props cause re-render?
A re-render can only be triggered if a component’s state has changed. The state can change from a props change, or from a direct setState change.