JS: Built-In Library

JS: Built-In Library

Introduction to Built-In Library of JavaScript.

ยท

4 min read

In JavaScript, built-in libraries refer to pre-defined sets of functionality that are included in the language itself. These libraries provide developers with a range of tools and functions that can be used to create powerful web applications. Here is a brief description of some of the main built-in libraries in JavaScript:

  1. Math - provides a set of mathematical operations including trigonometric functions, exponential, and logarithmic functions.

  2. String - provides a set of operations for working with strings, like converting the case of the string, extracting substrings, concatenating strings, replacing strings, and more.

  3. Array - provides many methods for working with arrays including sorting arrays, filtering arrays, mapping arrays, and more.

  4. Date - provides methods to work with dates, time and timezones.

  5. JSON - JSON stands for JavaScript Object Notation and provides a lightweight method for transferring data between applications, which is often used in web services and APIs.

  6. RegExp - allows to search for patterns within a string using regular expression patterns.

  7. Global - the global object contains properties that the JavaScript environment makes available to you, including the console object, which is used for logging messages.


Examples

Several examples of built-in library and usage:

Math

This library provides a set of mathematical operations including trigonometric functions, exponential, and logarithmic functions. Example Code:

Math.round(2.2); // returns 2
Math.pow(2,3); // returns 8
Math.sqrt(25); // returns 5

String

This library provides a set of operations for working with strings, like converting the case of the string, extracting substrings, concatenating strings, replacing strings, and more. Example Code:

let greeting = "Hello, World!";
console.log(greeting.toUpperCase()); // returns "HELLO, WORLD!"
console.log(greeting.substring(0, 5)); // returns "Hello"
console.log(greeting.replace("World", "Universe")); // returns "Hello, Universe!"

Array

This library is used to manipulate and work with arrays. It provides many methods for sorting arrays, filtering arrays, mapping arrays, and more. Example Code:

let fruits = ['banana', 'apple', 'orange'];
console.log(fruits.sort()); // returns ['apple', 'banana', 'orange']
console.log(fruits.filter(fruit => fruit === 'apple')); // returns ['apple']
console.log(fruits.map(fruit => fruit.toUpperCase())); // returns ['BANANA', 'APPLE', 'ORANGE']

Date

This library provides methods to work with dates, time, and timezones. Example Code:

let today = new Date();
console.log(today.toLocaleDateString()); // returns "7/26/2021"
console.log(today.getHours()); // returns current hour
console.log(today.getTimezoneOffset()); // returns difference between UTC and local time

JSON

JSON stands for JavaScript Object Notation and provides a lightweight method for transferring data between applications, which is often used in web services and APIs. Example Code:

let person = { name: "John", age: 30, city: "New York" };
let jsonPerson = JSON.stringify(person);
console.log(jsonPerson); // returns {"name":"John","age":30,"city":"New York"}
let parsedPerson = JSON.parse(jsonPerson);
console.log(parsedPerson.age); // returns 30

RegExp

RegExp - This library allows searching for patterns within a string using regular expression patterns. Example Code:

let str = "I love JavaScript";
let pattern = /javascript/i; // the "i" flag makes it case-insensitive
console.log(str.match(pattern)); // returns ['JavaScript']

Global

This library contains properties that the JavaScript environment makes available to developers, including the console object, which is used for logging messages. Example Code:

console.log("Hello, World!"); // prints "Hello, World!" to the console
console.error("This is an error message!"); // prints an error message to the console
console.warn("This is a warning message!"); // prints a warning message to the console

These built-in libraries are designed to be highly optimized, tested, and efficient, making them the best option for implementing certain functionality. Developers can use these built-in libraries in their programs to perform a wide variety of operations efficiently, adding robustness to their applications.


References

There are many great resources available online to learn more about the built-in libraries in JavaScript. Here are a few popular ones:

  1. Mozilla Developer Network (MDN) - MDN is a popular resource for learning JavaScript built-in libraries, providing detailed documentation with examples: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects

  2. W3Schools - W3Schools provides interactive tutorials on JavaScript built-in libraries with hands-on coding exercises: https://www.w3schools.com/js/js_builtin_functions.asp

  3. FreeCodeCamp - FreeCodeCamp provides a comprehensive guide to JavaScript built-in libraries, and it includes interactive coding challenges and quizzes: https://www.freecodecamp.org/news/javascript-built-in-functions/

  4. JavaScript.Info - This website provides a detailed explanation of JavaScript built-in libraries with in-depth examples and demos: https://javascript.info/js-builtins

By using these resources, you can gain a better understanding of JavaScript built-in libraries and how to use them to improve your programming skills.


Disclaimer: This article is created with ChatGPT using my prompts. I learn these things myself and I share the text as is. If you find errors please add comments so I can fix them.


Learn and prosper ๐Ÿ––

ย