What Are JavaScript Functions and Examples?

Blog Gönderisi Resmi

Click to get information about our services.

JavaScript, one of today's most popular programming languages, contains various functions that increase user experience by adding dynamism to web pages. These functions serve different purposes and are very important in the web development process. In this article, you will get detailed information about JavaScript functions and understand these functions better with examples.

Alert Function :

The Alert function is used to convey a specific message to the user by displaying a message box. This function is often used to display error messages or warning messages and provides users with a message box with several options.


 alert("This is a warning message.");
 

Confirm Function:

The Confirm function displays a message box for the user to confirm or reject an option. If the user confirms the option, the function returns true. If rejected, the function returns false.


var confirmValue = confirm("Do you want to confirm?");
if (confirmValue == true) {
     alert("Your selection has been confirmed.");
} else {
     alert("Your selection has been rejected.");
}

Prompt Function :

The prompt function displays a message box that prompts the user to enter text from a specific text box. The text entered by the user is returned in string format.


var name = prompt("Please enter your name: ");
alert("Hello " + name + "! Welcome.");

SetTimeout and SetInterval Functions:

SetTimeout and SetInterval functions are used to manage timing. SetTimeout function allows a particular function to be executed after a certain time. The SetInterval function allows a certain function to be repeated at certain intervals.


// SetTimeout function
setTimeout(function() {
     alert("Executed after a certain period of time.");
}, 5000); // will run after 5 seconds

// SetInterval function
var interval = setInterval(function() {
     alert("Repeats at a certain interval.");
}, 2000); // will be repeated every 2 seconds

//to stop the setInterval function
clearInterval(interval);

Math Functions :

Math functions are used to perform mathematical operations and are JavaScript's standard math library. These functions include operations such as taking absolute values, taking square roots, taking exponents, and generating random numbers.


// Getting absolute value
var x = Math.abs(-5); //5

//Taking square root
var y = Math.sqrt(25); //5

//exponentiation
var z = Math.pow(2, 3); //8

// Generating random numbers
var random = Math.floor(Math.random() * 10) + 1; //A random integer between 1-10

In conclusion, JavaScript functions are very important for web development and add dynamism to web pages. The functions mentioned in this article are the most basic JavaScript functions that can be used on web pages and can be better understood with example usage.

What We Recommend For You

Blog Resim
Vue.js'ye Başlamak ve Vue.JS Kurulumu

JavaScript

Vue.js son yıllarda popüler hale gelen JavaScript kütüphanesi ve framework'üdür. Özellikle Single Page Applications (SPA) oluşturma konusunda ..

Read More
Blog Resim
Vue.JS Nedir? Nerelerde Kullanılır?

JavaScript

Vue.JS, açık kaynak bir JavaScript hızlı geliştirme uygulama çerçevesidir. Vue.JS, tek sayfalı uygulamalar (SPA) oluşturmak için kullanılabilecek ..

Read More
Blog Resim
JavaScript Upper & Lowercase Conversion

JavaScript

".toUpperCase()", a term frequently encountered during SEO (Search Engine Optimization) studies, is a function used in the JavaScript language. This ..

Read More
Blog Resim
What is Next.js? What are the Advantages and Features?

JavaScript

Next.js, one of the most preferred tools by developers when creating web applications and sites, is a framework that works on the basis of React ..

Read More