JavaScript Basic concept (String & Array)

Sultan Mahmud
5 min readMay 5, 2021

--

JavaScript (JS) is a lightweight, interpreted, just-in-time compiled programming language with object-oriented capabilities that allows to build interactivity into otherwise static HTML pages.

JavaScript (JS)t is the most popular programming language in the world, so it’s no wonder that JavaScript is one of the most sought-after skills in the web development industry today.

In this blog I will discuss about JavaScript basic important concet String and Array.

#String

  1. A JavaScript string stores a series of characters.
  2. A string can be any text inside double or single quotes:

example : “we love our country” , “123455”

In this example “we love our country ” is a string and “123455” is also a string. One question genarate on your mind. that is :- “123455” is number but how is it a string??

Ans: when you write something inside double or single quotes that will be string.

Now I will talk about some method of string

1.length

length means total character inside double or single quotes. By using length property we can find total character. remember : space also a character;

lets, see an example:-

#2. charAt()

We can access an individual character in a string by using this method.

const name= “smr saimon khan”; Now if you want to access or find a specific character of this string you can use charAt() method.

let’s see an example:

In this example, const string2 =“I Love sports” ;

I use charAt(1),charAt(2),charAt(3), here 1,2,3 is a index number. Index start from 0 ;

Another way (introduced in ECMAScript 5) is to treat the string as an array-like object, where individual characters correspond to a numerical index:

example: console.log(string2[1]) ans: I

#3. replace()

We can replace a specifice word or character in string by using replace method. It’s take 2 paramiter, 1s paramiter: that need to remove and second paramiter: that need to add on same position.

Just think previous example that was “ I Love sports”, now we want to remove sports and add songs on same position. that will be “ I love sports”;

let’s see an example:

#4.trim()

The trim() method removes whitespace from both ends of a string. trim() is a important method of string.

let’s see an example:

In this example I have shwon before trim() a string condition and after trim a string condition.

#5.split()

The split() method is used to split a string into an array of substrings, and returns the new array.

If an empty string (“”) is used as the separator, the string is split between each character. The split() method does not change the original string.

let’s see an example:

#Array

In javascript array actually a special type of Object. We can declare an array by two way.

  1. const option1=new Array()
  2. const option2=[ ]

After declaration an array we can store value in array.

option2=[“ apple”, “mango”, “banana”]

Every value we can access by index. Now one question will be arise what is the index in array??

#6.IndexOf()

Ans: The index property is a red-only property. It contains the position of regular express every where and every programming language. It’s count start from 0;

let, you have 1 apple , 1 manago, and 1 banana and you store this foods by maintain secuence as like as 1st apple then manago then banana. Your 1st store iteam apple. what is the index number of apple??

Ans: Index will be 0. because Index start from zero (0). Same way your 2nd iteam idex will be 1 and 3rd index will be 2;

let’s see an example:

If you want to find something that not store in your target array then you will get index -1; index -1 means no result found.

#7. Push()

The push() method adds new items to the end of an array, and returns the new length. The new item(s) will be added at the end of the array. This method changes the length of the array.

let’s see an example:

#8. unshift()

The unshift() method adds new items to the beginning of an array, and returns the new length. The new item(s) will be added at the beginning of the array. This method changes the length of the array.

let’s see an example:

#9. pop()

The pop() method remove items to the end of an array, and returns the new length. This method changes the length of the array.

let’s see an example:

#10. shift()

The shift() method remove items to the beginning of an array, and returns the new length. This method changes the length of the array.

let’s see an example:

--

--