The .charAt(index) method returns the character at the specified index in a string.
let str = "Hello, World!";
let char = str.charAt(0);
console.log(char); // Output: "H"The .charCodeAt(index) method returns the Unicode of the character at the specified index in a string.
let str = "Hello, World!";
let charCode = str.charCodeAt(0);
console.log(charCode); // Output: 72
Example: Alphabet Position to Number Positon
function position(letter){
letter = letter.toLowerCase();
let positionABC = letter.charCodeAt(0) - 96;
return positionABC;
}The .codePointAt(position) method returns the Unicode code point value of the character at the specified position in a string.
let str = "Hello, World!";
let charCode = str.codePointAt(0);
console.log(charCode); // Output: 72The .concat(string1, string2, ...) method concatenates one or more strings to the calling string and returns a new string.
let str = "Hello";
let newStr = str.concat(", ", "World!");
console.log(newStr); // Output: "Hello, World!"The .endsWith(searchString, length) method returns a boolean indicating whether a string ends with the specified string at the specified position.
let str = "Hello, World!";
let result = str.endsWith("World!");
console.log(result); // Output: trueThe .includes(searchString, position) method returns a boolean indicating whether a string contains the specified string at the specified position.
let str = "Hello, World!";
let result = str.includes("World");
console.log(result); // Output: trueThe .indexOf(searchValue, start) method returns the first index at which a given element can be found in the array, or -1 if it is not present
let str = "Hello World!";
let index = str.indexOf("o");
console.log(index); // 4The .lastIndexOf(searchValue, start) method returns the last index at which a given element can be found in the array, or -1 if it is not present
let str = "Hello World! Hello World!";
let index = str.lastIndexOf("o");
console.log(index); // 15The .length property returns the number of characters in a string.
let str = "Hello, World!";
let strLength = str.length;
console.log(strLength); // Output: 13The .localeCompare(compareString) method compares the calling string to a specified string in the current locale and returns a number indicating whether the calling string is less than, equal to, or greater than the specified string.
let str1 = "Hello";
let str2 = "world";
let comparison = str1.localeCompare(str2);
console.log(comparison); // Output: -1 (str1 is less than str2 in lexicographic order)The .match(searchValue) method searches a string for a match against a regular expression, and returns the matches as an Array object.
let str = "Hello World! Hello World!";
let matches = str.match(/Hello/g);
console.log(matches); // ["Hello", "Hello"]The .repeat(count) method returns a new string with the specified number of copies of the original string.
function repeatStr (num, str) {
return str.repeat(num);
}
//Example:
let str = "Hello";
let newStr = str.repeat(3);
console.log(newStr); // Output: "HelloHelloHello"
//Example 2:
function repeatWord(num, str) {
let repeatedString = repeatStr(num, str);
console.log(repeatedString);
}
repeatWord(2, "Hello"); // "HelloHello"The .replace(searchValue, newValue) method replaces a specified value with another value in a string
let str = "Hello World!";
let newStr = str.replace("World", "Universe");
console.log(newStr); // "Hello Universe!"
Example:
Example 1: Replace string with any Word
function replaceWords(str, replaceWord) {
return str.replace(/\S+/g, replaceWord); // \S+ matches sequences of one or more non-whitespace characters, so replace every word with replaceWord
}
//Example 2: Ceaser Cypher Shift Positive or Negative
function caesar(str, shift) { // shift is the cypher # shifted positive or negative
const alphabet = 26; // the number of letters in the alphabet
shift = ((shift % alphabet) + alphabet) % alphabet; // ensure the shift value is always positive and less than alphabet
return str.replace(/[a-zA-Z]/g, (char) => { // use a regular expression to match all uppercase and lowercase letters
let code = char.charCodeAt(0) + shift; // get the unicode value of the current letter
if ((code > 90 && code < 97) || code > 122) { // check if the code is outside the range of uppercase or lowercase letters
code -= alphabet; // if it is, wrap it around the alphabet by subtracting alphabet
}
return String.fromCharCode(code); // return the shifted letter as a string
});
}
console.log(caesar("HELLO", 3)); // returns "KHOOR"The .replaceAll(searchValue, replaceValue) method searches a string for all occurrences of a specified value and returns a new string with all occurrences of the search value replaced with the replace value.
string.replaceAll(searchValue, replaceValue)
//Example
let str = "Hello World! World, it's a beautiful World!";
let newStr = str.replaceAll("World", "Earth");
console.log(newStr); // "Hello Earth! Earth, it's a beautiful Earth!"The .search(searchValue) method searches a string for a specified value and returns the position of the match.
let str = "Hello World!";
let index = str.search("World");
console.log(index); // 6The .slice(start, end) method returns a subset of a string between a start and end index (if you provide only one number, it will start from that index to the end of the string)
let str = "Hello World!";
let slc = str.slice(2, 8);
console.log(slc); // "llo Wo"The .split(separator) method splits a string into an array of substrings, using the specified separator.
let str = "Hello, World!";
let strArray = str.split(",");
console.log(strArray); // Output: ["Hello", " World!"]
//Example: Remove spaces
let str = "Hello World";
let newStr = str.split(' ').split('');
console.log(newStr); // "HelloWorld"The .startsWith(searchString, position) method returns a boolean indicating whether a string starts with the specified string at the specified position.
let str = "Hello, World!";
let result = str.startsWith("Hello");
console.log(result); // Output: trueThe .substr(start, length) method returns a subset of a string starting from a specified index and with a specified length
let str = "Hello World!";
let sub = str.substr(2, 6);
console.log(sub); // "llo Wo"The substring() method returns a subset of a string between a start and end index
let str = "Hello World!";
let sub = str.substring(2, 8);
console.log(sub); // "llo WThe .toLowerCase() method returns the calling string value converted to lowercase.
let str = "Hello, World!";
let newStr = str.toLowerCase();
console.log(newStr); // Output: "hello, world!"The .toString() method returns the value of a string object
let str = new String("Hello World!");
console.log(str.toString());The .toUpperCase() method returns the calling string value converted to uppercase.
let str = "Hello, World!";
let newStr = str.toUpperCase();
console.log(newStr); // Output: "HELLO, WORLD!"The .trim() method removes whitespace from the beginning and end of a string.
let str = " Hello, World! ";
let newStr = str.trim();
console.log(newStr); // Output: "Hello, World!"Determines whether the passed value is a finite number, returns a Boolean.
isFinite(Infinity)Determines whether the passed value is an integer, returns a Boolean.
isInteger(3.14) // returns falseDetermines whether the passed value is NaN (Not a Number), returns a Boolean.
isNaN(NaN) // returns true.Determines whether the passed value is a safe integer, returns a Boolean. Safe integers are integers that can be exactly represented in JavaScript.
isSafeInteger(9007199254740990) // returns false.Parses a string argument and returns a floating-point
parseFloat("3.14") // returns 3.14.s used to parse a string and return an integer. It takes two arguments: the string to be parsed and an optional radix, which specifies the base of the number in the string. The first argument can be any string, which will be co
parseInt("11", 2) // returns 3.Returns a string representing the number in exponential notation. The fractionDigits parameter is optional and specifies the number of digits after the decimal point.
(3.14).toExponential(1) // returns "3.1e+0".Returns a string representing the number with a specified number of decimal places.
(3.14159).toFixed(2) // returns "3.14".Returns a string representing the number using the current locale.
(3.14).toLocaleString // might return "3,14" in some locales.Returns a string representing the number with a specified number of significant digits.
(3.14159).toPrecision(2) // returns "3.1".Returns a string representing the The radix parameter (base number, binary, octal, hectal, etc) is optional and specifies the base of the returned string.
(11).toString(2) // returns "1011".Returns the primitive value of a Number object.
(new Number(3.14)).valueOf // returns 3.14.