Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link

JavaScript Built-in Functions

JavaScript is a popular scripting language that is used for creating dynamic web pages and web applications. It is known for its versatility and the ability to interact with HTML and CSS. One of the most useful features of JavaScript is the built-in functions that come with the language. These functions allow developers to perform common tasks without having to write custom code.

In this article, we will discuss the different types of built-in functions in JavaScript with examples.

  1. String Functions String functions are used to manipulate strings in JavaScript. Some of the common string functions are:
  • length() – returns the length of a string Example:
javascriptCopy codevar str = "Hello World!";
var length = str.length;
console.log(length); // outputs 12
  • toUpperCase() – converts a string to uppercase Example:
javascriptCopy codevar str = "Hello World!";
var uppercase = str.toUpperCase();
console.log(uppercase); // outputs "HELLO WORLD!"
  • toLowerCase() – converts a string to lowercase Example:
javascriptCopy codevar str = "Hello World!";
var lowercase = str.toLowerCase();
console.log(lowercase); // outputs "hello world!"
  • substring() – returns a portion of a string Example:
perlCopy codevar str = "Hello World!";
var sub = str.substring(0, 5);
console.log(sub); // outputs "Hello"
  1. Math Functions Math functions are used to perform mathematical operations in JavaScript. Some of the common math functions are:
  • round() – rounds a number to the nearest integer Example:
javascriptCopy codevar num = 4.7;
var rounded = Math.round(num);
console.log(rounded); // outputs 5
  • floor() – rounds a number down to the nearest integer Example:
javascriptCopy codevar num = 4.7;
var floor = Math.floor(num);
console.log(floor); // outputs 4
  • ceil() – rounds a number up to the nearest integer Example:
javascriptCopy codevar num = 4.2;
var ceil = Math.ceil(num);
console.log(ceil); // outputs 5
  • random() – generates a random number between 0 and 1 Example:
javascriptCopy codevar random = Math.random();
console.log(random); // outputs a random number between 0 and 1
  1. Array Functions Array functions are used to manipulate arrays in JavaScript. Some of the common array functions are:
  • push() – adds an element to the end of an array Example:
scssCopy codevar arr = [1, 2, 3];
arr.push(4);
console.log(arr); // outputs [1, 2, 3, 4]
  • pop() – removes the last element from an array Example:
scssCopy codevar arr = [1, 2, 3];
arr.pop();
console.log(arr); // outputs [1, 2]
  • shift() – removes the first element from an array Example:
scssCopy codevar arr = [1, 2, 3];
arr.shift();
console.log(arr); // outputs [2, 3]
  • unshift() – adds an element to the beginning of an array Example:
scssCopy codevar arr = [1, 2, 3];
arr.unshift(0);
console.log(arr); // outputs [0, 1, 2, 3]
  1. Date Functions Date functions are used to manipulate dates in JavaScript. Some of the common date functions are:
  • getDate() – returns the day of the month (1-31) for a specified date Example:
sqlCopy codevar date = new Date();
var day = date.getDate();
console.log(day); // outputs the day of the month
  • getMonth() – returns the month (0-11) for a specified date Example:
javascriptCopy codevar date = new Date();
var month = date.getMonth();
console.log(month); // outputs the month (0-11)
  • getFullYear() – returns the year (4 digits) for a specified date Example:
javascriptCopy codevar date = new Date();
var year = date.getFullYear();
console.log(year); // outputs the year (4 digits)
  • getTime() – returns the number of milliseconds since January 1, 1970, for a specified date Example:
javascriptCopy codevar date = new Date();
var time = date.getTime();
console.log(time); // outputs the number of milliseconds since January 1, 1970
  1. Regular Expression Functions Regular expression functions are used to search and manipulate text in JavaScript. Some of the common regular expression functions are:
  • test() – tests for a match in a string and returns true or false Example:
javascriptCopy codevar str = "Hello World!";
var pattern = /Hello/;
var result = pattern.test(str);
console.log(result); // outputs true
  • match() – searches for a match in a string and returns the result as an array Example:
javascriptCopy codevar str = "The quick brown fox jumps over the lazy dog.";
var pattern = /fox/;
var result = str.match(pattern);
console.log(result); // outputs ["fox"]
  • replace() – searches for a match in a string and replaces it with a specified value Example:
javascriptCopy codevar str = "Hello World!";
var pattern = /World/;
var replacement = "Universe";
var result = str.replace(pattern, replacement);
console.log(result); // outputs "Hello Universe!"

In conclusion, JavaScript built-in functions are extremely useful for developers as they provide a quick and easy way to perform common tasks. Understanding these functions and how to use them effectively can greatly enhance your productivity and help you create better JavaScript code.

Post a Comment