The primitive or special reference types in JS are created to ease up the operations with primitive values, such as the Boolean type, the Number type and the String type.
Traditional methods
All three primitive types contain these traditional methods:
METHOD | DESCRIPTION |
---|---|
valueOf() | Returns the value of the reference type. |
toLocaleString() | Returns the string representation of the reference type's value set for the local charset. |
toString() | Returns the string representation of the reference type's value. |
The Boolean type
The Boolean type is the reference type that corresponds to the Boolean value. To create an object, a Boolean constructor is used with TRUE or FALSE values passed in.
var boolObject = new Boolean(true);
The Number type
The Number type is the reference type that corresponds to the numeric values. To create an object, a Number constructor is used with a number value passed in.
var numberObject = new Number(number);
METHOD | DESCRIPTION |
---|---|
toFixed() | Returns a string represented as a number with a decimal point specified within apprentices. |
toExponential() | Returns a string represented as a number formatted in exponential notation; Decimal point is also specified within apprentices. |
toPrecision() | Returns either the fixed or exponential representation of the number depending on which makes more sense; The argument passed represents the number of digits to be used. |
The String type
The String type is the reference type that corresponds to the string values. To create an object, a String constructor is used with a string being passed in, as shown below:
var stringObject = new String("Happy new year!");
METHOD | DESCRIPTION |
---|---|
charAt() | Returns the character at the position given within the apprentices, starting with 0as the first character. |
charCodeAt() | Returns the character's (at the given position) character code, instead of the character itself. |
fromCharCodeAt() | Returns a character's code and converts it into a string. |
METHOD | DESCRIPTION |
---|---|
concat() | Concatenates one or more strings to another, returning the concatenated string as result. |
slice() | Returns a substring of a given string they are applied on. The second argument is the position before which capture is stopped. |
substr() | Returns a substring of a given string they are applied on. The second argument is the number of characters to return. |
substring() | Returns a substring of a given string they are applied on. The second argument is the position before which capture is stopped. |
METHOD | DESCRIPTION |
---|---|
indexOf() | Searches a given string for a substring and return -1 if it hasn't been found. Starts searching at the beginning of the string. |
lastIndexOf() | Searches a given string for a substring and return -1 if it hasn't been found. Starts at the end. |
METHOD | DESCRIPTION |
---|---|
localeCompare() | Compares one string to another and returns -1, 0, or 1. |
METHOD | DESCRIPTION |
---|---|
toLowerCase() | Converts case from lower to upper. |
toLocaleLowercase() | Converts case from lower to upper according to locale rules. |
toUpperCase() | Converts case from upper to lower. |
toLocaleUpperCase() | Converts case from upper to lower according to locale rules. |
METHOD | DESCRIPTION |
---|---|
match() | Accepts only one argument and it is used to extract groups of matched data |
search() | Returns the index of the first pattern occurrence in a string. |
replace() | Replaces the given pattern with a given string. |
split() | Separates a string into an array substrings based on a passed separator. |
Comments
No comments have been made yet.
Please login to leave a comment. Login now