Predefined functions for variables

Learn the most useful predefined functions for JavaScript variables. Discover how to use typeof, isNaN, toString, toFixed, length, includes, slice, replace, trim, and split to manage and manipulate numbers and strings effectively.

Published on 16 May 2026
Reading Time 2
Number of Words 390

Predefined functions for variables

JavaScript provides many built-in (predefined) functions that make it easier to work with variables of different types. These functions can be used to inspect, convert, or manipulate values directly.

Below, we’ll explore some of the most common predefined functions used with numeric and string variables.


Predefined Functions for Numeric Variables

These functions are useful when dealing with numbers, allowing you to check their type, convert them, or format them.

typeof

Returns the data type of a variable.

let num = 10; console.log(typeof num); // Output: "number"

This function is useful to verify whether a variable is a number, string, object, or another type.


isNaN()

Determines whether a value is not a number (NaN).

let num = 'hello'; console.log(isNaN(num)); // Output: true

The result is true because the value of num is a string, not a number.


toString()

Converts a numeric value into a string (text).

let number = 5; console.log(typeof number); // Output: "number" console.log(typeof number.toString()); // Output: "string"

After using toString(), the number is now stored as text.


toFixed()

Rounds a number to a specified number of decimal places.

let number = 5.5; console.log(number.toFixed()); // Output: "6" console.log(number.toFixed(3)); // Output: "5.500"

  • Without parameters, toFixed() rounds to the nearest integer.

  • When a parameter is provided (e.g., 3), it keeps that many decimal places.


Predefined Functions for String Variables

These functions are used to inspect and manipulate text values in JavaScript.

typeof

Indicates the type of the variable — in this case, "string".

let name_var = "JavaScript"; console.log(typeof name_var); // Output: "string"


length

Returns the number of characters in a string.

let name_var = "JavaScript"; console.log(name_var.length); // Output: 10


includes()

Checks whether a specific text is contained within a string.

let text = "Learn JavaScript today!"; console.log(text.includes("JavaScript")); // Output: true


slice()

Returns a portion of a string, using the starting and ending character positions.
The count begins at index 0.

let name_var = "JavaScript"; console.log(name_var.slice(2, 5)); // Output: "vaS"


replace()

Replaces one part of a string with another.

let text = "I love Jscr!"; console.log(text.replace("Jscr", "JavaScript")); // Output: "I love JavaScript!"


trim()

Removes leading and trailing whitespace from a string.

let text = " Hello JavaScript! "; console.log(text.trim()); // Output: "Hello JavaScript!"


split()

Divides a string into multiple parts based on a specified separator character.

let names = "John,Paul,George,Ringo"; console.log(names.split(",")); // Output: ["John", "Paul", "George", "Ringo"]


Summary

Function Type Description Example
typeof Number / String Returns variable type typeof num"number"
isNaN() Number Checks if value is not a number isNaN('hi')true
toString() Number Converts number to string 5.toString()"5"
toFixed() Number Rounds number to fixed decimals 5.5.toFixed(2)"5.50"
length String Counts string length "JS".length2
includes() String Searches for substring "JS".includes("J")true
slice() String Extracts part of string "Java".slice(1,3)"av"
replace() String Replaces text "JS".replace("J","K")"KS"
trim() String Removes spaces " JS ".trim()"JS"
split() String Splits string "a,b".split(",")["a","b"]