method and conapts
Why using const or let is best instead of var
What does “new “key word do while creating instance
- It creates at first a new empty object and assign “this “to this empty object.
- It returns that object from the constructor function.
- It makes prototype of empty object as the prototype of constructor function therefor we can access methods or variable declared in constructor prototype from newly created object.
Before you understand closure, you need to know at first another concept. When you create a scope inside a function with declare a function or any other block statement, you have always access to the parent scope. So inner function has access to outer function. This term has a special name that is lexical scope. Now let’s talk about closure. Look at this example.
How to access a character of a string
There are two ways of accessing a character
- Using charAt() method. It takes (0 based index) and returns the character as a string.
- In Es6 it is easier to perform that By using  notation for example:
let str = “sakib”str.charAt(0)=> "s"
str => "s"
Check if a string ends with a specified string or not
If you need to check the string ends with specified string or not you can use this method endsWith(). This method takes a string as a first argument and a length number as a second argument. Second argument is optional, if not specified, by default length number is string. length. This method returns true or false. For example: -
const str = “rakib ahmed”;str.endsWith('ahmed') => true
str.endsWith('radwan') => false
We often need to a string with some replacement of another string leaving original string unchanged. we can use replace () method. It takes two arguments first is: the string will be replaced second is: new substring. It returns a new string without changing original string.
Change case of string
toLowerCase() . This method will convert all alpha to lowercase without modifying original string.
toUpperCase(). This method will convert all alpha to uppercase without modifying original string. for example: -
const str = “SakiB”
str.toLowerCase() => "sakib"
str.toUpperCase() => " SAKIB "
How to remove whitespace from string
We often need to remove whitespaces of string. trim () this method removes all whitespace of beginning and end of string. trimStart() this method removes all whitespace of beginning of string. trimeEnd() this method removes all whitespace of ending of string.
What does every() Array method do?
every method takes a call back function as an argument and this function takes array value. This function returns a Boolean value. So if returns true on every value over looping, every method returns true otherwise return false.
reverse any array elements
There is a built-in array method that reverse an array element. So now you don't need to use loop to get reversed element. The method is Array.reverse(). It returns reversed array. For example:-
const arr = [1,2,3,4]
arr.reverse() => [4.3,2,1]