JavaScript Basic Concepts

MD.Rashaduzamman Rian
3 min readMay 5, 2021

01. Title: How to find Text and text position with JavaScript

Description:

We can find text through JavaScript with its string method called String.Includes(). This method returns Boolean (true and false). And its case sensitive.

Example:

const fullText = ‘Welcome to programming hero’;

const text = ‘hero’;

console.log(`word “${text}” ${fullText.includes(text) ? ‘is’ : ‘is not’} in the sentence`);

// expected output: “word “hero” is in the sentence”

Syntax for includes()

includes(searchString)

includes(searchString, position)

02. Title: How to search Text and text position with JavaScript

Description:

We can find text through JavaScript with its string method called String.indexOf(). And its case sensitive.

Example:

const fullText = ‘we have learned from programming hero, it creates hero of programming’;

const searchTerm = ‘hero’;

const indexOfFirst = fullText.indexOf(searchTerm);

console.log(`The index of the first “${searchTerm}” from the beginning is ${indexOfFirst}`);

// expected output: “The index of the first “hero” from the beginning is 33"

console.log(`The index of the 2nd “${searchTerm}” is ${fullText.indexOf(searchTerm, (indexOfFirst + 1))}`);

// expected output: “The index of the 2nd “hero” is 60"

03. Title: How to Remove White Space with JavaScript

Description:

We can remove white space from both side with JavaScript with its string method called String.trim(). There is also trimStart() and trimEnd(). By trimStart() method it will remove white space of starting string and by trimEnd() method it will remove white space of ending of string .

Example:

const greeting = ‘ Hello world! ‘;

console.log(greeting);

// expected output: “ Hello world! “;

console.log(greeting.trim());

// expected output: “Hello world!”;

04. Title: Using Array with JavaScript

Description:

Create a basic Array:

let fruits = ['Apple', 'Banana', ‘Mango’]console.log(fruits.length); //to find length of array// 3

Array Start with 0 and end with 99

let firstArray=fruits[0]

//console.log(firstArray);

//output- Apple

05. Title: How to Use Find method in JavaScript

Description:

find() method returns first matched element from Array if any value not matched its returned value of undefined.

Example:

const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found);

// expected output: 12

06. Title: How to Use Filter method in JavaScript

Description:

filter() method create new array from elements of array. Its returns Array.

Example:

const words = [‘spray’, ‘limit’, ‘elite’, ‘exuberant’, ‘destruction’, ‘present’];

const result = words.filter(word => word.length > 6);

console.log(result);

// expected output: Array [“exuberant”, “destruction”, “present”]

07. Title: How to Remove an element from an Array

Description:

pop() method removes last element from an array. This method will change the length of an array. And it will return that pop() element.

Example:

const plants = [‘broccoli’, ‘cauliflower’, ‘cabbage’, ‘kale’, ‘tomato’];

console.log(plants.pop());

// expected output: “tomato”

console.log(plants);

// expected output: Array [“broccoli”, “cauliflower”, “cabbage”, “kale”]

plants.pop();

console.log(plants);

// expected output: Array [“broccoli”, “cauliflower”, “cabbage”]

08. Title: How to Add an element end to an Array

Description:

push() method will add one or more then one element to the end of an array. And it will create new length of Array.

Example:

const animals = [‘pigs’, ‘goats’, ‘sheep’];

const count = animals.push(‘cows’);

console.log(count);

// expected output: 4

console.log(animals);

// expected output: Array [“pigs”, “goats”, “sheep”, “cows”]

animals.push(‘chickens’, ‘cats’, ‘dogs’);

console.log(animals);

// expected output: Array [“pigs”, “goats”, “sheep”, “cows”, “chickens”, “cats”, “dogs”]

09. Title: How to Remove an element first to an Array

Description:

shift() method will remove one or more then one element to the first of an array. And it will create new length of Array.

Example:

const array1 = [1, 2, 3];

const firstElement = array1.shift();

console.log(array1);

// expected output: Array [2, 3]

console.log(firstElement);

// expected output: 1

10. Title: How to Add an element beginning to an Array

Description:

unshift() method will add one or more then one element to beginning of an Array. And it will return new length of an Array.

Example:

const array1 = [1, 2, 3];

console.log(array1.unshift(4, 5));

// expected output: 5

console.log(array1);

// expected output: Array [4, 5, 1, 2, 3]

--

--