DEV Community

Kaushik V
Kaushik V

Posted on

2

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

Neon image

Serverless Postgres in 300ms (!)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free β†’

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay