Javascript - lishiying410/HTML-Notes GitHub Wiki
JS String:
-
When meeting special characters use backslash escape character to turn special character into string character. Eg, backslash
\'
will result in'
,\"
will result in"
,\\
will result in\
. -
String can be objects.
var x = "John";
var y = new String("John");
// (x == y) is true because x and y have equal values
// (x === y) is false because x and y have different types (string and object)
-
String methods, all string methods return a new string, they don't modify existing strings:
indexOf(), find the index of the first occurrence of a specific text in string.
search(), also search for text in a string like indexOf(), the difference is search() can take regular expression as parameter, and indexOf() can take the 2nd parameter which is the start searching position.
lastIndexOf(), find the index of the last occurrence of a specific text in a string.
For replacing string content, replace(contentNeedToBeReplaced, new content)
For extracting string parts, slice(start, end), substring(start, end), substr(start, length).
For extracting string characters, charAt(position) returns the character at specific position, charCodeAt(position) returns the unicode of the character at specific position.
Convert String to Array, split(separator)
JS Array
-
JS Array is Object, the way to create an array: var newArray= [item1,item2,item3,...];
-
Array Methods
Convert array to string, array.toString().
pop items out of an array, pop(), removes the last item of the array
shift(), remove the first item in an array, all other items will have one lower index.
unshift(), add an element to the array at beginning.
push item inside an array, push(), add an item to the array at the end
Add new items in an array, splice(param1, param2, param3), param1 is where new items should be added, params is how many items should be removed, param3 is the new elements needs to be added. So, when the params doesn't exist, this function can be used to remove elements in an array.
Merge arrays, array1.concat(array2, array3,...);
Slice out a piece of array to a new array, slice(startIndex,endIndex), endIndex is not included. and endIndex is optional.
Use Math.max(),Math.min() to find largest/smallest value in array.
JS Regular Expression
https://www.w3schools.com/jS/js_regexp.asp