sharing the knowledge of Arrays and Strings in Javascript:
*Arrays.js *
// 📌array decalration in js
let array=[1,2,3,4,5];
console.log(array);
//📌length of array
console.log(array.length);
//📌while loop
let i=0;
while(i
{
console.log("The element at ",i,"is->",array[i]);
i++;
}
//📌 In javascript we can add any data type in array , suppose we have a array of 1,2,3,4,5 amd we want to add "hello"
// which is a string , so we can add that in it
//📌📌push (adding to back), unshift(adding to front)
array.push("last value");
console.log(array);
//👉ans --> [ 1, 2, 3, 4, 5, 'last value' ]
array.unshift("first value");
console.log(array);
//👉 ans -->[ 'first value', 1, 2, 3, 4, 5, 'last value' ]
//📌📌pop(removing from last) ,shift(removing from front )
array.pop();
console.log(array);
//👉 ans -->[ 'first value', 1, 2, 3, 4, 5 ]
array.shift();
console.log(array);
// 👉ans -->[ 1, 2, 3, 4, 5 ]
// 📌📌indexof ,contains[see yourslef]
//📌📌slice(staring index,ending index)--> you can relate to as substring ,
let partofarray=array.slice(2,4);
console.log(partofarray);
// ans -->[ 3, 4 ] i.e isne last wale index se pehle ka maal bheja hai
let partofarray2=array.slice(2);
console.log(partofarray2);
// ans-->[ 3, 4, 5 ] i.e usne 2 se lekar saare end tak de diye
// 📌📌splice (staring,ending) --> it deletes from starting to ending index (it changes the array and not gives the copy array wih changes)
array.splice(2,3);
console.log(array);
//ans -->[ 1, 2 ] i.e it deletes from 2nd index and deletes 3 values from 2nd index (you can consider 3 as count and not the index)
//indexof()[basically gives the index of any element ] and contains()[it tells whether the element is present or not ]
Strings
// 📌STRINGS
//👉 i.e string can be declared in single and double quotes unlike java
let singlequote ='single quote ki string';
let doublequote ="double quote ki string";
console.log(singlequote);
console.log(doublequote);
//📌charAt
let char =singlequote.charAt(3);
console.log(char);
// ans-->g
//📌charCodeAt
// (basically it gives ascii value of the charAt )
let ascii=singlequote.charCodeAt(3);
console.log(ascii);
// ans-->103 (ascii value of g)
//📌substring
// (it gives substring as java )
let substr =singlequote.substring(2,8);
console.log(substr);
// ans-->ngle q
//📌split
// (it acts like split in java (stringBuilder) but here it gives array of strings as output)
let arrstr=singlequote.split("i");
console.log(arrstr);
// ans-->[ 's', 'ngle quote k', ' str', 'ng' ]
let nospace =singlequote.split("");
console.log(nospace);
// ans-->[
// 's', 'i', 'n', 'g', 'l',
// 'e', ' ', 'q', 'u', 'o',
// 't', 'e', ' ', 'k', 'i',
// ' ', 's', 't', 'r', 'i',
// 'n', 'g'
// ]
//📌 join
// (int arrstr1 we have splitted on the basis of " "(space) and after that we have joined on the basis of $ )
let arrstr1=singlequote.split(" ");
console.log(arrstr1);
let str=arrstr1.join("$")
console.log(str);
// ans-->[ 'single', 'quote', 'ki', 'string' ]
// single$quote$ki$string
//📌trim
// (basically it removes white spaces from front and end )
let ans =' hello my name is pushan ';
console.log(ans.trim());
// ans-->hello my name is pushan
// (you can see that ans has so many white spaces infront and at back , so trim removes it )
Top comments (0)