5 DialogBoxes
5 DialogBoxes
&
DIALOG BOXES
Using Template String
• ES6 has introduced a much simpler way of concatenating variable’s value inside a
message called Template String
EXAMPLE :
let x=10;
console.log(`Value of x is ${x}`);
Using Template String
• We can even write expressions or function calls inside template strings and they will
be evaluated by JS and the result of expression will be inserted in template string
EXAMPLE :
let x=10;
console.log(`Twice of ${x} is ${x*2}`);
EXAMPLE :
let x=16;
console.log(`Squareroot of ${x} is ${Math.sqrt(x)}`);
Using Dialog Boxes
JavaScript allows us to display messages using dialog boxes.
Like if an input field requires to enter some text but user does not enter that field
then as a part of validation we can use alert box to give warning message.
Syntax:
alert(‘message to show’);
Prompt Dialog Boxes
The prompt dialog box is very useful when we want to pop-up a text box to get user
input. Thus it enables us to interact with the user. The user needs to fill in the field and
then click OK.
Syntax:
prompt(‘message’,’default text’);
This dialog box is displayed using a method called prompt() which takes
two parameters:
If the user clicks on OK button the method prompt() will return entered value
from the text box. If the user clicks on the Cancel button the
method prompt() returns null.
Accepting Numeric Input
Since prompt( ) returns everything as a string so if we want to accept numbers then we need
to use either of 2 methods:
parseInt( ) parseFloat( )
Syntax:
If the user clicks on OK button the window method confirm() will return true. If the
user clicks on the Cancel button confirm() returns false.
Syntax:
confirm(‘question to ask’);
Important Point
• The methods alert( ),prompt( ) and confirm( ) are methods of window object.
• Thus they can be called using the notation window.<method name>( ) also.
• But since window object is the default object we can directly call these methods.