Welcome to the JavaScript Starter Methods and Functions repository! This project is designed for beginners who are looking to learn and understand fundamental JavaScript methods, functions, and definitions. The repository contains various examples and explanations of essential JavaScript array methods, string methods, and other utility functions that are frequently used in day-to-day programming.
This repository aims to provide a comprehensive collection of JavaScript methods and functions that are essential for beginners. Each method and function is explained with code examples, making it easier for new developers to grasp the concepts and start using them in their projects.
To get started with this repository, clone it to your local machine:
git clone https://github.com/penn201500/JS-Startup
You can view the source code files and run them in your preferred JavaScript environment.
The push method adds one or more elements to the end of an array and returns the new length of the array.
let array = [1, 2]
array.push("A", "B")
console.log(array) // Output: [1, 2, 'A', 'B']
The pop method removes the last element from an array and returns that element. This method changes the length of the array.
let array = [1, 2, "A", "B"]
console.log(array.pop()) // Output: 'B'
console.log(array) // Output: [1, 2, 'A']
The unshift method adds one or more elements to the beginning of an array and returns the new length of the array.
let array = [1, 2]
array.unshift("A", "B")
console.log(array) // Output: ['A', 'B', 1, 2]
The shift method removes the first element from an array and returns that element. This method changes the length of the array.
let array = ["A", "B", 1, 2]
console.log(array.shift()) // Output: 'A'
console.log(array) // Output: ['B', 1, 2]
The sort method sorts the elements of an array in place and returns the sorted array.
let array = [3, 1, 4, 1, 5]
array.sort()
console.log(array) // Output: [1, 1, 3, 4, 5]
The splice method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
let array = ["Microsoft", "Apple", "Yahoo", "AOL", "Excite", "Oracle"]
array.splice(2, 3, "Google", "Facebook")
console.log(array) // Output: ['Microsoft', 'Apple', 'Google', 'Facebook', 'Oracle']
The concat method is used to merge two or more arrays. This method does not change the existing arrays but instead returns a new array.
let array1 = [1, 2, 3]
let array2 = [4, 5, 6]
let array3 = array1.concat(array2)
console.log(array3) // Output: [1, 2, 3, 4, 5, 6]
The join method joins all elements of an array into a string and returns this string.
let array = ["Microsoft", "Apple", "Yahoo"]
let str = array.join("-")
console.log(str) // Output: 'Microsoft-Apple-Yahoo'
The slice method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array.
let array = ["apple", "banana", "cherry", "date", "fig"]
let slicedArray = array.slice(1, 3)
console.log(slicedArray) // Output: ['banana', 'cherry']
The charAt method returns the character at the specified index in a string.
let str = 'Hello World';
console.log(str.charAt(1)); // Output: 'e'
The concat method combines the text of two or more strings and returns a new string.
let str1 = 'Hello';
let str2 = 'World';
let str3 = str1.concat(' ', str2);
console.log(str3); // Output: 'Hello World'
The includes method determines whether one string may be found within another string, returning true or false as appropriate.
let str = 'Hello World';
console.log(str.includes('World')); // Output: true
The indexOf method returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.
let str = 'Hello World';
console.log(str.indexOf('World')); // Output: 6
The slice method extracts a section of a string and returns it as a new string, without modifying the original string.
let str = 'Hello World';
let slicedStr = str.slice(1, 5);
console.log(slicedStr); // Output: 'ello'
The split method splits a String object into an array of strings by separating the string into substrings.
let str = 'Hello World';
let arr = str.split(' ');
console.log(arr); // Output: ['Hello', 'World']
The parseInt function parses a string argument and returns an integer of the specified radix.
console.log(parseInt('10')); // Output: 10
console.log(parseInt('10.5')); // Output: 10
The parseFloat function parses a string argument and returns a floating point number.
console.log(parseFloat('10.5')); // Output: 10.5
console.log(parseFloat('10')); // Output: 10
The isNaN function determines whether a value is NaN (Not-a-Number).
console.log(isNaN('Hello')); // Output: true
console.log(isNaN(123)); // Output: false
The isFinite function determines whether a number is a finite, legal number.
console.log(isFinite(123)); // Output: true
console.log(isFinite(Infinity)); // Output: false
Feel free to fork this repository and make changes as needed. Contributions are welcome!
This project is licensed under the MIT License. See the LICENSE file for details.