A Guide to Javascript String and Closures

Md. Arifur Rahman
3 min readMay 5, 2021

String

  1. Extraction of the Parts of String:

There are three methods of sting extraction. These are:

a. Slice(start, end)

b. substring(start, end)

c. substr(start, length)

slice() method:

It extracts parts from the string and results a new string produced from the remained parts of after extraction. It needs two parameters: starting index(position) and end index(position). The example provided below shows the slice method:

Example:

const demoString = ‘A quick brown fox’;Const result = str.slice(2, 8);// output: quick

substring() method:

substring() and slice() methods are similar. The only difference is, substring() cannot have a negative index.

Example:

const demoString = ‘A quick brown fox’;Const result = str.slice(2, 8);// output: quick

substring() method:

substring() is similar to substring() and slice() method. The only difference is, substring() specify the length of the extracted portion of the string.

2. Replacing the Content of String

replace() method:

replace() method replace a portion of the string with another string.

Example:

const demoString = ‘Welcome to google’;const n = str.replace(‘google’, ‘yahoo’);

replace() method can accept regEx for search value.

3. Converting to Upper case and Lower case

toUpperCase() method:

A string is converted to upper case with the method toUpperCase();

Example:

const demoText1 = ‘You are learning Javascript’;const demoText2 = text1.toUpperCase();//demoText2 is demoText1 but converted to upper case

4. Joining of String

concat() method:

Two or more strings can be joined together with concat() method.

Example:

const demoText1 = ‘Learning’;const demoText2 = ‘Javascript’;demoText3 = demoText1.concat(‘ ’, demoText2);

5. Get content from String

There are two safe method of getting content from a string:

a. charAt(position)

b. charCodeAt(position)

charAt() method:

charAt() method provides the value of the specified index of the string.

charCodeAt() method:

charCodeAt() method provides the unicode value of the specified index of the string.

6. Search in string

search() method:

To search in string search() method is used. The part which is to be searched is set with the regex. If finds the result it shows the position otherwise return -1.

Closures:

A child function always can access the variables, functions and objects of the parent function. But here a interesting fact is that if we do return the parent function, we can still access the variables, functions and objects from the child function. This is called closures. This happens because the variables. Functions and objects remains in the memory even after returning the parent function.

This is the default behaviour of javascript. Now, how the Closures work? We know we can return a function from the inside of another function and parent child relationship occurres. And child function can access the whole things from the returned parent function.

const demoClosures =() => {    const x = 1;    return function () {        const y = 2:        console.log(‘Sum: ‘ + (x + y));    }}

Here, we can see that the child function is accessing the variable x even though the parent function already has returned.

demoClosures()();

Here, the inner child function is anonymous and is being returned directly. We can use different name of the child function but there is no benefit ot do it. So, in our first call the first function will be executed and then the child function will be executed. Fo this reason, we used double braces here.

We also can pass the arguments to the parent function can access the arguments from the child function as well.

So, this is a bit about Closures. Lets see you later in an another amazing topic.

--

--