JavaScript Functions
JavaScript is a popular programming language used for both frontend and backend development. It offers a wide range of built-in functions and allows you to create custom functions to perform specific tasks. Here are a few examples of JavaScript functions:
toUpperCase Function
The toUpperCase function is used to convert a string to uppercase. It returns a new string with all characters converted to uppercase. Here's an example:
const str = 'hello, world!';
const uppercaseStr = str.toUpperCase();
In the above example, the toUpperCase function is applied to the str variable, converting it to uppercase.
sort Function
The sort function is used to sort the elements of an array in ascending order. It modifies the original array. Here's an example:
const numbers = [5, 2, 8, 1, 4];
numbers.sort();
In the above example, the sort function is applied to the numbers array, sorting its elements in ascending order.
Custom Function: calculateArea
You can also create custom functions in JavaScript. Here's an example of a custom function to calculate the area of a rectangle:
function calculateArea(length, width) {
return length * width;
}
const length = 5;
const width = 3;
const area = calculateArea(length, width);
In the above example, the calculateArea function takes two parameters, length and width, and returns the calculated area.
These are just a few examples of the functions available in JavaScript. The language provides a vast array of built-in functions and allows you to create custom functions to suit your specific needs.