D Dev Notebook

JavaScript Functions

JS

A function is a block of code designed to perform a particular task. Functions allow you to organize your code, make it reusable, and improve readability.

Advantage of JavaScript function

Code reusability: We can call a function several times so it save coding.

Less coding: It makes our program compact. We don't need to write many lines of code each time to perform a common task.

Declare a function

function hello(){
	console.log('hello function')
}

Call the function

hello()

Example

function welcome(){
	console.log('hello, welcome to Dev Notebook')
}
welcome()

Examples

function add(a, b) {
    return a + b;
}
add(10, 20);


function aboutMe(name, age) {
    document.write (name + " is " + age + " years old.");
}
aboutMe('Digital', 7)

On this page