Main functions applicable in Javascript variables
The built-in functions in JavaScript are common methods that can be applied in a general way to variables. These functions can be differentiated in whether they apply to variables of numeric or string type:
- Predefined functions for numeric type variables:
- typeof: indicates the content type of a variable
letnum = 10;
console.log(typeofnum);
- isNaN: indicates if a variable is not a number
letnum = 'hello';
console.log(isNaN(num));
It will give us true as a result since the value of num is not a number - toString: Converts a value from a number to a text or string type.
let number = 5;
console.log(typeofnumber);
console.log(typeof number.toString());
The second result will give us that it is of type string having converted it. - toFixed(): Round a number to the given value.
let number = 5.5;
console.log(number.toFixed());
In this case, round without decimals.
Result: 6
let number = 5.5;
console.log(number.toFixed(3));
In this case round to 3 decimals
Result: 5,500
- Predefined functions for variables of type string:
- typeof. Indicates the content type of the variable.
Example: typeof name_var
- length. Measures the number of characters in a string.
Example: name_var.length
- includes. Checks that a specific text is included in a variable.
Example: name_var.includes('text_to_search')
- slice. Returns part of a string indicating the initial and final character number, counting the initial character from 0.
Example: name_var.slices(2,5)
- replace. Replaces one text with another within a variable.
Example: name_var.replace('ub', 'jscr')
→replace jscr with ub - trim. Remove leading and trailing whitespace within a variable.
Example: name_var.trim()
- split. Splits a text string into parts starting at a specified character.
Example: name_var.split(',')
→Splits the name_var string into multiple parts using the comma as a reference.
Tips on SEO and Online Business