Math.sqrt() Method in JavaScript: A Method to Get the Square Root

How do we use the Math.sqrt() method to get the square root of a number in JavaScript?

In this article, you’ll learn how to calculate square roots with ease and boost your coding skills.

Aside from that, you’ll find out how to square a number in JavaScript. Read on to explore more!

Let’s get started to find out how to calculate the square root of a number in JavaScript using the Math.sqrt() method.

What is Math.sqrt in JavaScript?

The Math.sqrt() is a built-in static method in JavaScript that is used to calculate the square root of a number.

The term “static” means that the method belongs to the Math object itself, and not to an instance of the Math object.

That’s the reason why we call it using Math.sqrt() and not on a Math object that we created.

What is math sqrt syntax?

Here’s the syntax of Math,sqrt() method:

Math.sqrt(number) ✅

What is the Parameter?

This function takes one parameter:

number

This is the number you want to find the square root of. It should be a non-negative number.

What is the return value?

The Math.sqrt() method returns the square root of the number provided. If the number is negative, it returns NaN (Not a Number).

Supported Browser

✔ Chrome

✔ Edge

✔ Firefox

✔ Internet Explorer

✔ Safari

✔ Opera

How does Math.sqrt () method work?

The sqrt() method short for “square root”, is a mathematical method that returns the square root of a given number.

In other words, it finds a value that, when multiplied by itself, gives the original number.

Here’s a simple example: the square root of 49 is 7 because 7 * 7 equals 49.

In JavaScript, you can use the Math.sqrt() method to calculate the square root of a number.

Here’s how it works:

let samplenumber = 64;
console.log(Math.sqrt(samplenumber));

Output:

8

If you try to find the square root of a negative number, the function will return NaN, which stands for “Not a Number,” because square roots of negative numbers are not real numbers.

Here’s an example:

let samplenumber = -64;
console.log(Math.sqrt(samplenumber));

Output:

NaN

Here are the other examples:

Math.sqrt(-81); // NaN
Math.sqrt(-0); // -0
Math.sqrt(0); // 0
Math.sqrt(1); // 1
Math.sqrt(2); // 1.414213562373095
Math.sqrt(16); // 4
Math.sqrt(Infinity); // Infinity

Frequently Asked Question (FAQs)

How to square in JavaScript?

You can square a number in JavaScript using the exponentiation operator (**).

Here’s an example:

let samplenum = 9;
let square = samplenum ** 2; ✅
console.log(square);

Output:

81

In our example, 9 ** 2 returns 81, because 9 * 9 is equal to 81.

What is the square root of 2 in JavaScript?

You can calculate the square root of a number in JavaScript using the Math.sqrt() method.

Here’s how you can find the square root of 2:

let samplenumber = 2;
console.log(Math.sqrt(samplenumber)); ✅

Output:

1.4142135623730951

As you can see in our example Math.sqrt(2) returns approximately 1.4142135623730951, because that is the square root of 2.

Conclusion

We’ve explored the Math.sqrt() method in JavaScript, which is used for calculating the square root of a number.

We’ve discussed its syntax, parameters, and return values and provided various examples.

Also, we illustrate how to square a number in JavaScript and calculate the square root of 2.

Please note that the Math.sqrt() is a static method, meaning it belongs to the Math object itself and not to an instance of the Math object.

It returns NaN for negative numbers as they do not have real square roots.

We are hoping that this article provides you with enough information that help you understand the Math.sqrt() method in JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Leave a Comment