DEV Community

PUSHAN VERMA
PUSHAN VERMA

Posted on

1

Javascript Day 2 ,3 and 4

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 )

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay