Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Array findIndex()

The findIndex() method returns the index of the first element in an array that meets the criteria specified in a test function.

For example, this code finds the index of the first element that is greater than 18:

Example

const numbers = [49162529];
let first = numbers.findIndex(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}

Keep in mind that the function accepts three arguments:

  • The value of the item
  • The index of the item
  • The array itself

New Math Methods

ES6 introduced the following methods to the Math object:

  • Math.trunc()
  • Math.sign()
  • Math.cbrt()
  • Math.log2()
  • Math.log10()

The Math.trunc() Method

The Math.trunc(x) method returns the integer portion of x, removing any fractional digits.

Example

Math.trunc(4.9);    // returns 4
Math.trunc(4.7);    // returns 4
Math.trunc(4.4);    // returns 4
Math.trunc(4.2);    // returns 4
Math.trunc(-4.2);    // returns -4

The Math.sign() Method

The Math.sign(x) method returns the sign of x, indicating whether it is negative, zero, or positive.

Example

Math.sign(-4);    // returns -1
Math.sign(0);    // returns 0
Math.sign(4);    // returns 1

The Math.cbrt() Method

The Math.cbrt(x) method returns the cube root of x.

Example

Math.cbrt(8);    // returns 2
Math.cbrt(64);    // returns 4
Math.cbrt(125);    // returns 5

The Math.log2() Method

The Math.log2(x) method returns the logarithm of x with base 2.

Example

Math.log2(2);    // returns 1