DEV Community

Kavya S
Kavya S

Posted on

JavaScript Return Statement

Return Statement
The return statement in JavaScript is used to end the execution of a function and return a value to the caller.

Syntax
return [expression]

Example

function add(a, b) {
        return a + b; // Returns the sum of a and b
    }

    let result = add(5, 3); //result will be 8
    console.log(result);
Enter fullscreen mode Exit fullscreen mode

Explanation

  • This code defines a function add,that takes two parameters a and b, and returns their sum.
  • When add(5, 3) is called, it passes 5 and 3 as arguments, computes 5 + 3, and returns 8.
  • The result (8) is stored in the variable result.

Prompt() Function
The prompt function is used to get user input and respond based on whether the user provided input or cancelled the dialog.

Example

let name=prompt("please enter ur name");
     if(name !==null){
        console.log("hello",name);
     }
     else{
        console.log("you cancelled the input");
     }
Enter fullscreen mode Exit fullscreen mode

in this example,

  • if user enters "Kavya" and clicks ok: Output:"hello Kavya"
  • if user clicks cancel the dialog box: Output:"you cancelled the input"

Alert() Function
The alert() function in JavaScript displays a message dialog box to the user with an OK button.

Syntax
alert(message);

Example
alert("Hello world!")

Top comments (0)