π§ Built-in JavaScript Functions Overview
JavaScript provides several built-in functions that help in tasks such as manipulating data, interacting with users, and handling errors. These functions are pre-defined in JavaScript and can be used directly without having to write custom code.
1. π’ alert()
The alert()
function is used to display a dialog box with a message. Itβs often used for debugging or notifying users.
alert("Message to display");
Example:
alert("Hello, Welcome to JavaScript!");
2. π¬ prompt()
The prompt()
function displays a dialog box with a text input field, allowing users to enter a value.
let userInput = prompt("Please enter your name:");
Example:
let name = prompt("Enter your name:");
alert("Hello, " + name + "!");
3. β
confirm()
The confirm()
function displays a dialog box with OK and Cancel buttons. It returns true
if the user clicks "OK" and false
if "Cancel" is clicked.
let result = confirm("Are you sure?");
Example:
let proceed = confirm("Do you want to continue?");
if (proceed) {
alert("You clicked OK!");
} else {
alert("You clicked Cancel!");
}
4. π’ parseInt()
The parseInt()
function is used to convert a string to an integer (whole number).
let number = parseInt("123");
Example:
let number = parseInt("123");
console.log(number); // Output: 123
5. π’ parseFloat()
The parseFloat()
function converts a string to a floating-point number.
let floatNumber = parseFloat("3.14");
Example:
let number = parseFloat("3.14");
console.log(number); // Output: 3.14
6. β isNaN()
The isNaN()
function checks whether a value is NaN (Not-a-Number). It returns true
if the value is NaN, and false
if itβs a number.
let check = isNaN("Hello");
console.log(check); // Output: true
7. π Math.random()
The Math.random()
function generates a random floating-point number between 0 (inclusive) and 1 (exclusive).
let randomNum = Math.random();
Example:
let randomValue = Math.random();
console.log(randomValue); // Output: A random number between 0 and 1
8. π’ Math.floor()
The Math.floor()
function rounds a number down to the nearest integer.
let roundedNumber = Math.floor(3.75);
Example:
let result = Math.floor(3.75);
console.log(result); // Output: 3
9. π’ Math.ceil()
The Math.ceil()
function rounds a number up to the nearest integer.
let roundedNumber = Math.ceil(3.14);
Example:
let result = Math.ceil(3.14);
console.log(result); // Output: 4
10. π Math.max()
and Math.min()
These functions return the largest and smallest values from a list of numbers, respectively.
let maxValue = Math.max(a, b, c);
let minValue = Math.min(a, b, c);
Example:
let max = Math.max(1, 4, 7, 3);
console.log(max); // Output: 7
let min = Math.min(1, 4, 7, 3);
console.log(min); // Output: 1
11. β° setTimeout()
The setTimeout()
function calls a function or evaluates an expression after a specified number of milliseconds.
setTimeout(function, delay);
Example:
setTimeout(function() {
alert("This is a delayed message!");
}, 2000); // This will show an alert after 2 seconds
12. β³ setInterval()
The setInterval()
function repeatedly calls a function with a fixed time delay between each call.
setInterval(function, interval);
Example:
let counter = 0;
setInterval(function() {
counter++;
console.log(counter);
}, 1000); // This will increment and log the counter every 1 second
13. π clearInterval()
and clearTimeout()
These functions are used to stop a timer set by setInterval()
or setTimeout()
.
clearInterval(intervalID);
clearTimeout(timeoutID);
Example:
let intervalID = setInterval(function() {
console.log("This will be stopped soon!");
}, 1000);
// Stop the interval after 5 seconds
setTimeout(function() {
clearInterval(intervalID);
console.log("Interval stopped!");
}, 5000);
14. π JSON.parse()
and JSON.stringify()
JSON.parse()
converts a JSON string into a JavaScript object, while JSON.stringify()
converts a JavaScript object into a JSON string.
let obj = JSON.parse(jsonString);
let jsonString = JSON.stringify(object);
Example:
let jsonString = '{"name": "John", "age": 30}';
let obj = JSON.parse(jsonString);
console.log(obj.name); // Output: John
let newObject = { name: "Alice", age: 25 };
let jsonStringified = JSON.stringify(newObject);
console.log(jsonStringified); // Output: {"name":"Alice","age":25}
π Conclusion
JavaScript offers a wide range of built-in functions that simplify various programming tasks. These functions allow you to manipulate data, interact with users, and perform mathematical calculations efficiently. Mastering these functions will make your code more concise, readable, and powerful.