JavaScript Built-in Functions

JavaScript comes with an extensive collection of built-in functions, allowing developers to perform various operations efficiently and with minimal effort. Here are some essential built-in functions and demonstrate how they can improve our coding experience.

Generic

  • isNaN - check is an expression is Not a Number:
isNaN("Hello");   // true
isNaN(42);        // false
isNaN(NaN);       // true
isNaN(undefined); // true
isNaN(null);      // false
  • eval - evaluates a string as JavaScript code and executes it; should be used with caution, due to security risks if used with untrusted input:
eval("2 + 5");                         // returns: 7
eval("console.log('Hello World!');");  // prints: Hello World!
  • parseInt - converts a string into an integer; takes an optional base to be used for conversion:
parseInt("42");         // returns: 42
parseInt("42 apples");  // returns: 42
parseInt("apples 42");  // returns: NaN
parseInt("101010",2);   // returns: 42
parseInt("101010",16);  // returns: 1052688
  • parseFloat - converts a string into a floating-point number:
parseInt("42.1 C");  // returns: 42.1
parseInt("C 42.1");  // returns: NaN

Object Prototype

  • assign - copy values from one or more source objects to a target object
  • create - creates a new object with the specified prototype object and properties
  • keys - returns an array of a given object's property names
  • values - returns an array of a given object's property values
  • entries - returns an array of a given object's own enumerable property [key, value] pairs
const target = { a: 1 };
const source1 = { b: 2 };
const source2 = { c: 3 };

const result = Object.assign(target, source1, source2);
console.log(result);  // prints: { a: 1, b: 2, c: 3 }

Object.keys(result);    // returns: ['a', 'b', 'c']
Object.values(result);  // returns: [1, 2, 3]
Object.entries(result); // returns: [['a', 1], ['b', 2], ['c', 3]]

Object.freeze(result);
result.a = 2; // This assignment will be silently ignored
console.log(result.a); // 1

  • freeze - freezes an object, preventing any changes to its properties
const result = { a: 1, b: 2, c: 3 };

Object.freeze(result);
result.a = 2; // This assignment will be silently ignored
console.log(result.a); // 1
  • seal - seals an object, preventing any new properties from being added to it and marking all existing properties as non-configurable
const result = { a: 1, b: 2, c: 3 };
Object.seal(result);

result.a = 2; // This assignment is allowed
result.b = 3; // This assignment will be silently ignored
console.log(result); // { a: 2 }

Math

Math functions provide essential mathematical operations and utilities. Some vital functions are:

  • Math.round() - rounds to nearest integer.
  • Math.floor() - rounds downward to nearest integer.
  • Math.ceil() - rounds a number upward to the nearest integer.
  • Math.random() - returns a random number between 0 and 1.
  • Math.min() and Math.max() - return the minimum and maximum values among the arguments, respectively.

Date

Date functions in JavaScript help you work with dates and time. Some essential date functions include:

  • Date.now() - returns number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.
  • getDate() - returns the day of the month for the specified date.
  • getMonth() - returns the month for the specified date.
  • getFullYear() - returns the year for the specified date.
  • setTime() - sets the date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970.

JSON

JSON functions facilitate the conversion between JSON strings and JavaScript objects:

  • JSON.parse() - parses a JSON string and converts it into a JavaScript object.
  • JSON.stringify() - converts a JavaScript object or value to a JSON string.

Continue here for practical JSON examples.


Others

All the built-in objects come with lots of predefined methods.