DEV Community

Cover image for Math functions and Searching elements
Kamalesh AR
Kamalesh AR

Posted on

Math functions and Searching elements

The Math Object

The JavaScript Math object allows you to perform mathematical tasks.

The Math object is static.

All methods and properties can be used without creating a Math object first.

Math Properties (Constants)

The syntax for any Math property is : Math.property.

JavaScript provides 8 mathematical constants that can be accessed as Math properties:

Math.E        // returns Euler's number
Math.PI       // returns PI
Math.SQRT2    // returns the square root of 2
Math.SQRT1_2  // returns the square root of 1/2
Math.LN2      // returns the natural logarithm of 2
Math.LN10     // returns the natural logarithm of 10
Math.LOG2E    // returns base 2 logarithm of E
Math.LOG10E   // returns base 10 logarithm of E
Enter fullscreen mode Exit fullscreen mode

Math Methods

Math.round()
Math.round(x) returns the nearest integer

Math.round(4.6);
Enter fullscreen mode Exit fullscreen mode

Math.ceil()
Math.ceil(x) returns the value of x rounded up to its nearest integer

Math.ceil(4.9);
Math.ceil(4.7);
Math.ceil(4.4);
Math.ceil(4.2);
Math.ceil(-4.2);
Enter fullscreen mode Exit fullscreen mode

Math.floor()
Math.floor(x) returns the value of x rounded down to its nearest integer

Math.floor(4.9);
Math.floor(4.7);
Math.floor(4.4);
Math.floor(4.2);
Math.floor(-4.2);
Enter fullscreen mode Exit fullscreen mode

Math.trunc()
Math.trunc(x) returns the integer part of x

Math.trunc(4.9);
Math.trunc(4.7);
Math.trunc(4.4);
Math.trunc(4.2);
Math.trunc(-4.2);
Enter fullscreen mode Exit fullscreen mode

Math.sign()
Math.sign(x) returns if x is negative, null or positive.

If x is positive it returns 1
If x is negative it returns -1
If x is zero, it returns 0

Math.sign(-4);
Math.sign(0);
Math.sign(4);
Enter fullscreen mode Exit fullscreen mode

Math.pow()
Math.pow(x, y) returns the value of x to the power of y

Math.pow(8, 2);
Enter fullscreen mode Exit fullscreen mode

Math.sqrt()
Math.sqrt(x) returns the square root of x

Math.sqrt(64);
Enter fullscreen mode Exit fullscreen mode

Math.abs()
Math.abs(x) returns the absolute (positive) value of x

Math.abs(-4.7);
Enter fullscreen mode Exit fullscreen mode

Math.min() and Math.max()
Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments

Math.min(0, 150, 30, 20, -8, -200);
Enter fullscreen mode Exit fullscreen mode
Math.max(0, 150, 30, 20, -8, -200);
Enter fullscreen mode Exit fullscreen mode

Math.random()
Math.random() returns a random number between 0 (inclusive), and 1 (exclusive)

Math.random();
Enter fullscreen mode Exit fullscreen mode

Finding HTML Elements

Often, with JavaScript, you want to manipulate HTML elements.

To do so, you have to find the elements first. There are several ways to do this:

Finding HTML elements by id
Finding HTML elements by tag name
Finding HTML elements by class name
Finding HTML elements by CSS selectors

Finding HTML Element by Id

The easiest way to find an HTML element in the DOM, is by using the element id.

This example finds the element with id="intro"

const element = document.getElementById("intro");
Enter fullscreen mode Exit fullscreen mode

If the element is found, the method will return the element as an object (in element).

If the element is not found, element will contain null.

Finding HTML Elements by Tag Name

This example finds all

elements

const element = document.getElementsByTagName("p");
Enter fullscreen mode Exit fullscreen mode

This example finds the element with id="main", and then finds all

elements inside "main"

const x = document.getElementById("main");
const y = x.getElementsByTagName("p");
Enter fullscreen mode Exit fullscreen mode

Finding HTML Elements by Class Name

If you want to find all HTML elements with the same class name, use getElementsByClassName().

This example returns a list of all elements with class="intro".

const x = document.getElementsByClassName("intro");
Enter fullscreen mode Exit fullscreen mode

Finding HTML Elements by CSS Selectors

The querySelector() Method

The Document method querySelector() returns the first Element within the document that matches the specified CSS selector, or group of CSS selectors. If no matches are found, null is returned.

The matching is done using depth-first pre-order traversal of the document's nodes starting with the first element in the document's markup and iterating through sequential nodes by order of the number of child nodes.

If the specified selector matches an ID that is incorrectly used more than once in the document, the first element with that ID is returned.

Parameters

selectors

A string containing one or more selectors to match. This string must be a valid CSS selector string; if it isn't, a SyntaxError exception is thrown.

Note that the HTML specification does not require attribute values to be valid CSS identifiers. If a class or id attribute value is not a valid CSS identifier, then you must escape it before using it in a selector, either by calling CSS.escape() on the value, or using one of the techniques described in Escaping characters. See Escaping attribute values for an example.

<html>
<body>

<p class="demo"></p>

<script>
// Access a paragraph Element
const myPara = document.querySelector(".demo");

// Change the content of the Element
myPara.innerHTML = "Hello World!";
</script>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The querySelectorAll() Method

The Document method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors.

Parameters

selectors

A string containing one or more selectors to match. This string must be a valid CSS selector string; if it isn't, a SyntaxError exception is thrown.

Note that the HTML specification does not require attribute values to be valid CSS identifiers. If a class or id attribute value is not a valid CSS identifier, then you must escape it before using it in a selector, either by calling CSS.escape() on the value, or using one of the techniques described in Escaping characters. See Escaping attribute values for an example.

<html>
<body>

<p class="demo">One</p>
<p class="demo">Two</p>

<script>
// Access a paragraph Element
const myItems = document.querySelectorAll(".demo");

// Change the content of the Element
myItems[0].innerHTML = "First";
</script>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

If you want to find all HTML elements that match a specified CSS selector (id, class names, types, attributes, values of attributes, etc), use the querySelectorAll() method.

This example returns a list of all

elements with class="intro".

Common Mistakes

Using # in getElementById():
Wrong: "#demo"
Forgetting that querySelector() returns only the first match

References:

https://www.w3schools.com/Js/js_htmldom_elements.asp
https://www.w3schools.com/jS/js_math.asp
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector

Top comments (0)