DEV Community

SELVAKUMAR R
SELVAKUMAR R

Posted on

Getting Input from the user in Java script

In java script block variable can't able to access in global in function.

We using the return keyword to access the block content globally in function

Ex:

let plate = makeBriyani(Rice);

console.log(plate);

function makeBriyani(container){

container = "rice";

return "Briyani";
}

output:

Briyani

Enter fullscreen mode Exit fullscreen mode

Getting the input from the user :

We using the prompt() function to get the input from the user

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        let age = prompt("Enter the Age");
        if(age >=18){
            console.log("You is eligible for vote");
        }
        else{
            console.log("You is not eligible for vote"); 
        }
    </script>
</head>
<body>

</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Task:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        let mark = prompt("Enter the mark");

        console.log(mark);

        if(mark>=90){
            console.log("Your grade is A+");
        }

        else if(mark>=80 ){
            console.log("Your grade is A");
        }

        else if(mark>=70){
            console.log("Your grade is B+");
        }

        else if(mark>=60 ){
            console.log("Your grade is B");
        }

         else if(mark>=50 ){
            console.log("Your grade is C");
        }

        else{
            alert("Your are fail");
        }
    </script>
</head>
<body>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)