DEV Community

Kaushik V
Kaushik V

Posted on

Javascript Basics

Common JavaScript Built-in Functions Explained

JavaScript has many built-in functions that help with selecting, modifying content and performing calculations. Here are some examples of such built-in functions.

DOM Manipulation Functions

  • getElementById(id) – Selects a single HTML element by its id. This is one of the most common ways to interact with elements in the DOM.
  • getElementsByClassName(className) – Selects all elements that have the specified class name(s). Returns an HTMLCollection.
  • getElementsByTagName(tagName) – Returns all elements in the document that match a specific tag name (e.g., div, p).
  • querySelector(selector) – Selects the first element that matches a given CSS selector.
  • createElement(tagName) – Creates a new HTML element dynamically using JavaScript.

Number Parsing Functions

  • parseInt(string) – Converts a string into an integer (whole number).
  • parseFloat(string) – Converts a string into a floating-point number (decimal).

Function Binding and Array Methods

  • bind() – Creates a new function that remembers a specific context, similar to putting a sticky note on a function to remind it of a specific this value.
  • push(value) – Adds an item to the end of an array.
  • pop() – Removes the last item from an array.
  • sort() – Sorts an array in ascending or descending order.
  • shift() – Removes the first item from an array.
  • unshift(value) – Adds an item to the beginning of an array.

Math Functions

  • Math.abs(number) – Converts a number to its absolute (positive) value.
  • Math() – A built-in JavaScript object that provides various mathematical functions and constants.

Other Useful Functions

  • getRootNode() – Retrieves the root (top-most) element in the DOM tree.
  • console.error(message) – Displays an error message in the browser’s developer console.
  • addEventListener(event, function) – Listens for a specific event (like a click) on a webpage and executes a function when the event occurs.
  • eval(string) – Executes JavaScript code written inside a string (Use with caution due to security risks).
  • escape(string) – Encodes a string for safe transfer over the internet.
  • document.write(content) – Writes content directly onto a webpage (Not recommended for modern web development).
  • length – Returns the number of elements in an array or the number of characters in a string

Top comments (0)