In Every Programming Language, we use string to store text format data for code reusability. There are many useful operations in string, but today I want to discuss two code fundamental concept which is charAt and concat operation in string.
charAt: This operation is used to access an individual character in the string. The charAt() method returns the specific data in the string. For example-
const names = ‘Iqbal Ahmed’;
Here charAt method starts searching that what’s the value at index 2? we all know that its start count from 0 then it’s find ‘b’ at index 2.
concat: String concatenation works to join two individual strings in one string. concat() method always returns a new string. For Example-
const firstName = ‘Iqbal ’;
const lastName = ‘Ahmed’;
const result = firstName.concat(lastName)
So the output will show ‘Iqbal Ahmed’.
Not only two you can join three or more as you want like this way.
const str1 = ‘HTML ’;
const str2 = ‘CSS ’;
const result = str1.concat(str2, str3);
parseFloat: The parseFloat function parses a string and returns to a floating-point number. Remember that it returns a number as a number if the first character in a specified string is a number then parse the string until the end of the number.
const number1 = parseFloat(“20.20”);
Here “20.20” is a string but when the parseFloat method work on it then its converts to 20.20 as a number value.
But if the program is like that-
const number2 = parseFloat(“I am 22”);
then the output will be NAN because it starts parsing from index 0 and the first value in the string is ‘I’ which is not a number then it’s return NAN.
Follow the program below-
const num1 = parseInt( “40”);
the output will be number 40. But if the program is like that
const num2 = parseInt(“The number is 40”) then the output will show NAN.
An array is a special type of variable that can store more than one value. You can say an array is a basic data structure. If I say write a program which can store your 5 friends name then you declare 5 variables and store every single value but in array, you can declare only one variable and store 5 friends name in one variable.
Creating an array
const names = [‘Tangil’, ’Nahid’, ’Habib’, ‘Sayem’, ’Sayeed’];
Another way creating an array.
const names = new Array (‘Tangil’, ’Nahid’, ’Habib’, ‘Sayem’, ’Sayeed’);
There are many methods in array but I’m discussing a few of them.
Push: Array push method is used for adding an item end of the array. Suppose I have a list where store some fruits name and now I want to insert another fruit name in the list then I need to use an array push method. See the program below-
let fruitsName = [‘banana’, ‘apple’, ‘orange’];
then the list will be like this [‘banana’, ‘apple’, ‘orange’, ‘mango’];
Pop: Array pop method is used for removing the last element from an array. From the previous program, there are four fruits in the fruits array banana, apple, orange, and mango. If I want to remove the last element I need to use the pop method.
let fruitsName = [‘banana’, ‘apple’, ‘orange’, mango];
then the output will be banana, apple, orange. The last element mango removed from the list.
Shift: Array shift method removed the first element from an array, just reverse of pop method.
const num = [20,30,40];
const newArray = num.shift())
// expected output [30,40]
Unshift: Array unshift method add one or more item in the beginning part of an array.
const numbers = [23,43,56,76];
// expected output 15
// expected output 10,15,23,43,56,76
Find: Array find method return the first value from the satisfied condition. We often use find for searching implementation.
const numbers = [10, 20, 30, 40., 50];
const res = array.find(number => number > 15);
// expected output 20
Filter: Array filter method create a new array from the satisfied condition. Array find method return only first value but filter method return all of the satisfied value.
const numbers = [10, 20, 30, 40., 50];
const res = array.filter(number => number > 15);
// expected output 20,30,40,50
So this is for today. Hope you guys understand. Keep Learning!!