Tutorial Details

Most imporant 40 JavaScript functions
20 Sep

Most imporant 40 JavaScript functions

1. alert()

Displays an alert dialog with the specified message and an OK button.

Example: alert("Hello World!");

2. console.log()

Outputs a message to the web console, useful for debugging.

Example: console.log("Message logged!");

3. setTimeout()

Executes a function after a specified number of milliseconds.

Example: setTimeout(function() { console.log("Timeout executed!"); }, 2000);

4. setInterval()

Repeatedly calls a function or evaluates an expression at specified intervals (in milliseconds).

Example: setInterval(function() { console.log("Interval running!"); }, 1000);

5. clearTimeout()

Clears a timeout set with setTimeout().

Example: let timeoutId = setTimeout(() => {}, 1000); clearTimeout(timeoutId);

6. clearInterval()

Clears an interval set with setInterval().

Example: let intervalId = setInterval(() => {}, 1000); clearInterval(intervalId);

7. parseInt()

Parses a string and returns an integer.

Example: parseInt("100"); // returns 100

8. parseFloat()

Parses a string and returns a floating-point number.

Example: parseFloat("10.5"); // returns 10.5

9. isNaN()

Determines whether a value is NaN (Not-a-Number).

Example: isNaN("hello"); // returns true

10. typeof

Returns the data type of a variable.

Example: typeof 42; // returns "number"

11. Object.keys()

Returns an array of a given object's property names.

Example: Object.keys({name: 'John', age: 30}); // returns ["name", "age"]

12. Object.values()

Returns an array of a given object's property values.

Example: Object.values({name: 'John', age: 30}); // returns ["John", 30]

13. Object.assign()

Copies all enumerable properties from one or more source objects to a target object.

Example: Object.assign(target, source);

14. Array.map()

Creates a new array by applying a function to every element in the original array.

Example: [1, 2, 3].map(x => x * 2); // returns [2, 4, 6]

15. Array.filter()

Creates a new array with all elements that pass a test implemented by a function.

Example: [1, 2, 3, 4].filter(x => x > 2); // returns [3, 4]

16. Array.reduce()

Reduces an array to a single value by applying a function to each element.

Example: [1, 2, 3].reduce((acc, curr) => acc + curr, 0); // returns 6

17. Array.forEach()

Executes a function once for each array element, but does not return a value.

Example: [1, 2, 3].forEach(x => console.log(x));

18. Array.find()

Returns the first element in the array that satisfies a provided testing function.

Example: [1, 2, 3].find(x => x > 2); // returns 3

19. Array.findIndex()

Returns the index of the first element in the array that satisfies the provided testing function.

Example: [1, 2, 3].findIndex(x => x > 2); // returns 2

20. Array.some()

Tests whether at least one element in the array passes the test implemented by the provided function.

Example: [1, 2, 3].some(x => x > 2); // returns true

21. Array.every()

Tests whether all elements in the array pass the test implemented by the provided function.

Example: [1, 2, 3].every(x => x > 0); // returns true

22. Array.concat()

Merges two or more arrays and returns a new array.

Example: [1, 2].concat([3, 4]); // returns [1, 2, 3, 4]

23. Array.push()

Adds one or more elements to the end of an array and returns the new length.

Example: let arr = [1, 2]; arr.push(3); // arr is now [1, 2, 3]

24. Array.pop()

Removes the last element from an array and returns that element.

Example: let arr = [1, 2, 3]; arr.pop(); // returns 3, arr is now [1, 2]

25. Array.shift()

Removes the first element from an array and returns that element.

Example: let arr = [1, 2, 3]; arr.shift(); // returns 1, arr is now [2, 3]

26. Array.unshift()

Adds one or more elements to the beginning of an array and returns the new length.

Example: let arr = [2, 3]; arr.unshift(1); // arr is now [1, 2, 3]

27. Array.slice()

Returns a shallow copy of a portion of an array into a new array object.

Example: let arr = [1, 2, 3]; arr.slice(1); // returns [2, 3]

28. Array.splice()

Changes the contents of an array by removing or replacing existing elements.

Example: let arr = [1, 2, 3]; arr.splice(1, 1); // removes element at index 1, arr is now [1, 3]

29. Math.random()

Returns a random floating-point number between 0 (inclusive) and 1 (exclusive).

Example: Math.random(); // returns a number like 0.234567

30. Math.floor()

Rounds a number downwards to the nearest integer.

Example: Math.floor(2.9); // returns 2

31. Math.ceil()

Rounds a number upwards to the nearest integer.

Example: Math.ceil(2.1); // returns 3

32. Math.max()

Returns the largest of zero or more numbers.

Example: Math.max(1, 2, 3); // returns 3

33. Math.min()

Returns the smallest of zero or more numbers.

Example: Math.min(1, 2, 3); // returns 1

34. Date()

Returns the current date and time as a string or creates a new Date object.

Example: new Date(); // returns current date

35. JSON.stringify()

Converts a JavaScript object or value to a JSON string.

Example: JSON.stringify({ name: "John" }); // returns '{"name":"John"}'

36. JSON.parse()

Parses a JSON string and returns the corresponding JavaScript object.

Example: JSON.parse('{"name":"John"}'); // returns { name: 'John' }

37. Promise.then()

Attached are callbacks for the resolution or rejection of a Promise.

Example: promise.then(result => { console.log(result); });

38. Promise.catch()

Attached is a callback for only the rejection of a Promise.

Example: promise.catch(error => { console.log(error); });

39. fetch()

Fetches resources asynchronously over the network and returns a Promise.

Example: fetch('https://api.example.com/data')

40. addEventListener()

Attaches an event handler to the specified element.

Example: element.addEventListener('click', function() { console.log('clicked!'); });

 

0 Comments

Leave a Comment