Let’s learn some important concepts and methods of JavaScript

Why using const or let is best instead of var

Basically, unlike other language there is no way to create scope in JavaScript except function block. So, for loop and if… else..else don't create scope. That’s creates a lot of unexcepted bug in our code. But const and let (That is Es6 features) solve this problem completely. If you declare with const or let inside a just curly braces, it is not accessible from out this block. So do not use var forget about it.

What does “new “key word do while creating instance

  1. It creates at first a new empty object and assign “this “to this empty object.
  2. It returns that object from the constructor function.
  3. 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.

What is closure in JavaScript?

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.

function  

How to access a character of a string

There are two ways of accessing a character

  1. Using charAt() method. It takes (0 based index) and returns the character as a string.
  2. In Es6 it is easier to perform that By using [] notation for example:
let str = “sakib”str.charAt(0)=> "s"
str[0] => "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

replace string

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]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
MD Sakib Ahmed

MD Sakib Ahmed

I am a MERN stack web developer. I love programing. Learn new tanchnology is my hobby