
Basic JavaScript Concept(string, number, array)
##Sring
In JavaScript, the textual data is stored in a string that is wrapped by a single quote(‘) or double quote(“).
let singleQuote = ‘single quote’;
let doubleQuote = “double quote”;
#String indexOf()
The string object’s indexOf() method returns a number value that gives the index number of the first letter of that word. Index number starts from 0.
Example:
let example = ‘Bangladesh is a very beautiful country. There are 64 districts in our country.’
let findIndex = ‘very“;
console.log(example.indexOf(findIndex));
// It will return value16 . Index number count starts from the letter “B”. White space is also counted in index number.;
// If that word not exist in the sentence it will return -1;
#String chartAt()
The string object’s charAt() method returns the letter of the index number position. Or use a squire bracket way to get the character at the position.
Example:
let str = “Hello World”;
console.log(str[3]); // answer will be “L”.
Or conole.log(str.charAt(3)); // “L”
Most commonly used squire bracket([]) way. That is the modern way. While charAt() exists mostly for historical reasons.
There is one difference between the two of them. If no character is found, [] returns undefined, and charAt returns an empty string:
Example:
let str = “Hello World”;
console.log(str[100]); // undefine
conole.log(str.charAt(100)); // empty string “ “
#String slice()
The string object’s slice() creates a new string from the old string part without modifying the original string.
str.slice(where form start, where to end) // basic structure .
It returns the part of the string from start to (but not including) end.
Example:
let str = “Hello World”
str.slice(0, 7); // “Hello Wo”
If there is no second argument, then slice() goes till the end of the string:
Example:
str.slice(2); // “llo World”
Negative values for start/end are also possible. They mean the position is counted from the string end:
Example:
str.slice(-3, -1) // “rl”
// start at the 3rd position from the right, end at the 1st from the right
##Number
#Math.abs()
The number object abs() method always returns an absolute value of the number. If the number is negative it will convert it into a positive number.
Example:
let number = -10;
console.log(Math.abs(number)) // 10.
If the number is positive it will return a positive value of the number.
#Math.Floor()
The number object floor() method always returns the largest integer less than or equal to a given number. That’s mean if number is 1.9, it will return 1, event if number is 1.01, it will also return 1,
Example:
let number = 1.2;
console.log(Math.floor(number)); // 1;
let number2 = 1.99
console.log(Math.floor(number2)); // 1
#Math.ceil()
The number object ceil() method always returns the integer number greater than or equal to a given number. That’s mean if number is 2.9, it will return 3, event if number is 2.01, it will also return 3,
Example:
let number = 3.2;
console.log(Math.ceil(number)); // 4;
let number2 = 3.99
console.log(Math.ceil(number2)); // 4
###Array
It’s a collection of data where stores multiple data like strings, numbers, objects. Separated by comma and wrapped by squire bracket [].
#find()
The find() method returns the value of the first element in an array that passes a test. If it finds an array element where the function returns a true value, find() returns the value of that array element and does not check the remaining values. Otherwise, it returns undefined
Example:
const array1 = [5, 12,17, 2, 10, 8, 13, 44 ];
const found = array1.find(element => element > 10);
console.log(found); // return 12;
#filter()
The filter() method calls a call-back function once for each of the array elements and creates a new array with all the values which the call-back function returns true. It means it constructs a new array with all elements that the call-back function’s condition returns true.
Example:
const array = [5, 12,17, 2, 10, 8, 13, 44 ];
const filterData = array.filter(element => element > 10);
console.log(found); // return[12, 17, 13, 44];
#push()
The push() method is a way to add new elements in an array. It adds an element to the end of the array and returns a new length. You can push multiple elements at a time.
Example:
const oldArray = [12, 1, 23, 54, 2, 22, 29];
const newArray = oldArray.push(5);
// return [12, 1, 23, 54, 2, 22, 29, 5]
#shift()
The shift() method removes the first element from the array and returns a new length. It creates a new length of the array.
Example:
const oldArray = [12, 1, 23, 54, 2, 22, 29];
console.log(oldArray.shift()); // 12
console.log(oldArray) // return [ 1, 23, 54, 2, 22, 29 ]