Revisit 10 topics in JavaScript
In this article, we’re going to revisit 10 topics in JavaScript that are frequently asked in interviews.
1. slice()
The JavaScript String slice() method extracts and returns a section of a string.
The slice()
method takes in:
- start (optional) — Starting index of the selection. If not provided, the selection starts at start 0.
- end (optional) — Ending index of the selection (exclusive). If not provided, the selection ends at the index of the last element.
- Returns a new array containing the extracted elements.
let languages = ["JavaScript", "Python", "C", "C++", "Java"];
// slicing the array (from start to end)
let new_arr = languages.slice();
console.log(new_arr); // [ 'JavaScript', 'Python', 'C', 'C++', 'Java' ]
// slicing from the third element
let new_arr1 = languages.slice(2);
console.log(new_arr1); // [ 'C', 'C++', 'Java' ]
// slicing from the second element to fourth element
let new_arr2 = languages.slice(1, 4);
console.log(new_arr2); // [ 'Python', 'C', 'C++' ]
Output
[ 'JavaScript', 'Python', 'C', 'C++', 'Java' ]
[ 'C', 'C++', 'Java' ]
[ 'Python', 'C', 'C++' ]
2. trim()
The JavaScript trim() method removes whitespace from both ends of a string.
The trim()
method does not take in any parameters.
- Returns a new string representing the
str
stripped of whitespace from both ends.
Notes:
- Whitespace is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
- The
trim()
method does not change the original string.
let str = " foo ";
console.log(str.trim()); // 'foo'// trim() removes whitespace only from the edges
let str1 = " A B C D ";
console.log(str1.trim()); // 'A B C D'
Output
foo
A B C D
3. startsWith()
The JavaScript String startsWith() method checks if a string begins with the characters of a specified string.
The startsWith()
method takes in :
- searchString — The characters to be searched for at the beginning of str.
- position (optional) — The position in str to start searching for searchString. Default value is 0.
And,
- Returns
true
if the given characters are found at the beginning of the string. - Returns
false
if given characters are not found at the beginning of the string.
Note: The startsWith()
method is case sensitive.
sentence = "Java is to JavaScript what Car is to Carpet.";let check = sentence.startsWith("Java");
console.log(check); // truelet check1 = sentence.startsWith("Java is");
console.log(check1); // true// case sensitive
let check2 = sentence.startsWith("JavaScript");
console.log(check2); // false// second argument specifies the starting position
let check3 = sentence.startsWith("JavaScript", 11);
console.log(check3); // true
Output
true
true
false
true
4. lastIndexOf()
The JavaScript String lastIndexOf() method returns the last index of occurance of a given value in the string, or -1 if it is not present.
The lastIndexOf()
method takes in:
- searchValue — The value to search for in the string. If no string is provided explicitly, fromIndex is returned.
- fromIndex (optional) — The index to start searching the string backwards. By default it is +Infinity.
Notes:
- If fromIndex >= string.length, the whole string is searched.
- If fromIndex < 0, it is considered to be the same as 0.
- Returns the last index of the value in the string if it is present at least once.
- Returns -1 if the value is not found in the string.
var str = "JavaScript is the world's most misunderstood programming language.";
// lastIndexOf() returns the last occurance
var index1 = str.lastIndexOf("language");
console.log(index1); // 57
var index2 = str.lastIndexOf("p");
console.log(index2); // 45
// second argument specifies the search's start index
var index3 = str.lastIndexOf("p", 44);
console.log(index3); // 8
// lastIndexOf returns -1 if not found
var index4 = str.lastIndexOf("Python");
console.log(index4); // -1
Output
57
45
8
-1
5.charAt()
The JavaScript String charAt() method returns the character at the specified index in a string.
The charAt()
method takes in :
- index — An integer between 0 and str.length — 1. If index cannot be converted to integer or is not provided, the default value 0 is used.
And
- Returns a string representing the character (exactly one UTF-16 code unit) at the specified index.
Note: charAt()
returns an empty string if index is out of range.
let sentence = "Happy Birthday to you!";
let index1 = sentence.charAt(2);
console.log("Character at index 2:" + index1); // 'p'
// index is converted to integer
let index2 = sentence.charAt(6.5); // 'B'
console.log("Character at index 6:" + index2);
// Empty string if index out of range
let index3 = sentence.charAt(100);
console.log("Character at index 100:" + index3); // ''
// default value is 0
let index4 = sentence.charAt();
console.log("Character at index 0:" + index4); // 'H'
Output
Character at index 2: p
Character at index 6: B
Character at index 100:
Character at index 0: H
6. toUpperCase()
The JavaScript String toUpperCase() method returns the string converted to uppercase.
The toUpperCase()
method does not take in any parameters.
Returns a new string representing the calling string converted to uppercase.
Notes:
- The
toUpperCase()
method raisesTypeError
when called onnull
orundefined
. - The
toUpperCase()
method does not change the original string.
let str = "Hello World!";
let sentence = "Java is to JavaScript what Car is to Carpet.";let uppercase_str = str.toUpperCase();
console.log(uppercase_str); // HELLO WORLD!let uppercase = sentence.toUpperCase();
console.log(uppercase); // JAVA IS TO JAVASCRIPT WHAT CAR IS TO CARPET.
Output
HELLO WORLD!
JAVA IS TO JAVASCRIPT WHAT CAR IS TO CARPET.
7. toLowerCase()
The JavaScript String toLowerCase() method returns the string converted to lowercase.
The toLowerCase()
method does not take in any parameters.
- Returns a new string representing the calling string converted to lowercase.
Notes:
- The
toLowerCase()
method raisesTypeError
when called onnull
orundefined
. - The
toLowerCase()
method does not change the original string.
let str = "Hello World!";
let sentence = "Java is to JavaScript what Car is to Carpet.";
let lowercase_str = str.toLowerCase();
console.log(lowercase_str); // hello world!
let lowercase = sentence.toLowerCase();
console.log(lowercase); // java is to javascript what car is to carpet.
Output
hello world!
java is to javascript what car is to carpet.
8. includes()
The JavaScript String includes() method checks if one string can be found inside another string.
The includes()
method takes in:
- searchString — A string to be searched for within str.
- position (optional) — The position within str to begin searching for searchString. By default, it is 0.
- Returns true if searchString is found anywhere within str.
- Returns false if searchString is not found anywhere within str.
Note: The includes()
method is case sensitive.
let sentence = "Java is to JavaScript what Car is to Carpet.";let check = sentence.includes("Java");
console.log(check); // true// case sensitive
let check1 = sentence.includes("java");
console.log(check1); // false// second argument specifies position to start at
let check2 = sentence.includes("Java", 20);
console.log(check2); // falselet check3 = sentence.includes("whose");
console.log(check3); // falselet check4 = sentence.includes("");
console.log(check4); // true
Output
true
false
false
false
true
9. split()
The JavaScript split() method divides a String into an ordered list of substrings and returns them as an array.
The split()
method takes in:
- separator (optional) — The pattern (string or regular expression) describing where each split should occur.
- limit (optional) — A non-negative integer limiting the number of pieces to split the given string into.
- Returns an
Array
of strings, split at each point where the separator occurs in the given string.
Note: The split()
method does not change the original string.
console.log("ABCDEF".split("")); // [ 'A', 'B', 'C', 'D', 'E', 'F' ]const text = "Java is awesome. Java is fun.";let pattern = ".";
let newText = text.split(pattern);
console.log(newText); // [ 'Java is awesome', ' Java is fun', '' ]let pattern1 = ".";
// only split string to maximum to parts
let newText1 = text.split(pattern1, 2);
console.log(newText1); // [ 'Java is awesome', ' Java is fun' ]const text2 = "JavaScript ; Python ;C;C++";
let pattern2 = ";";
let newText2 = text2.split(pattern2);
console.log(newText2); // [ 'JavaScript ', ' Python ', 'C', 'C++' ]// using RegEx
let pattern3 = /\s*(?:;|$)\s*/;
let newText3 = text2.split(pattern3);
console.log(newText3); // [ 'JavaScript', 'Python', 'C', 'C++' ]
Output
[ 'A', 'B', 'C', 'D', 'E', 'F' ]
[ 'Java is awesome', ' Java is fun', '' ]
[ 'Java is awesome', ' Java is fun' ]
[ 'JavaScript ', ' Python ', 'C', 'C++' ]
[ 'JavaScript', 'Python', 'C', 'C++' ]
10.substring()
The JavaScript String substring() method returns a specified part of the string between start and end indexes.
The substring()
method takes in:
- indexStart — The index of the first character to start including in the returned substring.
- indexEnd (optional) — The index before which to stop extraction. (Exclusive) If omitted, it extracts till the end of the string.
Notes:
- Any argument value < 0 is treated as 0.
- Any argument value > str.length is treated as str.length.
- Any
NaN
argument value is treated as 0. - If
indexStart
is greater thanindexEnd
, the two arguments are swapped, i.e.str.substring(a, b)
will bestr.substring(b, a)
. - Returns a new string containing the specified part of the given string.
Note: substring()
does not change the original string.
let string = "Programiz JavaScript Tutorials";// first character
substr1 = string.substring(0, 1);
console.log(substr1); // P// if start > end, they are swapped
substr2 = string.substring(1, 0);
console.log(substr2); // P// From 11th to last character
substr3 = string.substring(10);
console.log(substr3); // JavaScript Tutorials// the extreme values are 0 and str.length// same as string.substring(0)
substr4 = string.substring(-44, 90);
console.log(substr4); // Programiz JavaScript Tutorials// indexEnd is exclusive
substr5 = string.substring(0, string.length - 1);
console.log(substr5); // Programiz JavaScript Tutorial
Output
P
P
JavaScript Tutorials
Programiz JavaScript Tutorials
Programiz JavaScript Tutorial
Thank you for reading :)
/* Source:https://www.programiz.com/ */