Output for ECMAScipt 2015
ECMAScript Basics – Part 2
Foreword: In this part of the series, I explain how to output or display ECMAScript Information.
By: Chrysanthus Date Published: 12 May 2015
The Alert Box
ECMAScript has a box, which you can use to display ECMAScript information. This box usually appears like a small rectangle and it is usually gray in color. The syntax to display information is:
You have the word “alert”. After the word you have parentheses. Inside the parentheses you have text in single or double quotes. It is this text that is displayed in the alert box. The whole line above that ends with a semi-colon, is an example of what is called an ECMAScript statement. The word “alert” with its parentheses is an example of what is called a function.
Assume that you want to display the following string:
“I love ECMAScript.”
The statement you type, would be:
alert("I love ECMAScript.");
It could also be:
alert('I love ECMAScript.');
The first alert statement uses double quotes. The second one uses single quotes. At this point, I should warn you about quotations. The double or single quotation marks offered by your word processor (e.g. Microsoft Word), are not the ones you should use in your code. The quotation marks you use should be the ones offered by your text editor. The quotation marks offered by your text editor are not the same in shape as the ones offered by your word processor.
The following code will display an alert box with the content of the string, "I love ECMAScript."
alert('I love ECMAScript.');
Try the code. The actual script is within an HTML document (code). When you try the code, you should see the alert box, with the sentence. You may have to click the OK button of the alert box to dismiss it.
You can write information into the current page. The syntax for the statement is:
You begin with the word, “document”; this is followed by a dot, then the word, “write”, and then, parentheses. Inside the parentheses you have a string. The word, document, here, represents what is called an Object, in ECMAScript. The word, “write” with parentheses is another example of what is called a function.
The following code will insert the sentence, “I love ECMAScript.” into the current page:
document.write('I love ECMAScript.');
Try the code. In the code, the string is in single quotes. You can still use double quotes. The content of the string is the message of interest. At the browser, the string is inserted at the position where the ECMAScript is.
A statement in ECMAScript is a piece of code that usually ends with a semicolon.
You should have comments in your code. Comments are not executed. Comments are to remind you later of why you typed a particular piece of code. There are two types of comments: single-line comment and multiple-line comment. A single-line comment can only be in one line; something like:
// This is a single-line comment.
A single-line comment begins with a double forward slash. For a single-line comment, everything to the right of the comment is not executed.
A multiple-line comment begins with /* and ends with */ . An example is:
/* This is a multiple-line comment. It can be of any length, and
you can place whatever you want here. */
A multiple-line comment spans more than one line. The opening delimiter is a forward slash and asterisk. The closing delimiter is an asterisk and forward slash.
Try the following code:
//Talking about a man.
document.write("I am a man.<br />");
/*Talking about a girl.
and only the girl*/
document.write("She is a girl.<br />");
You can use ECMAScript to replace the content of an HTML element. You can use it to replace the content of a paragraph element, for example. You need to understand other ECMAScript principles first, before you can understand how to do this. So we shall look at the replacement possibilities later.
Let us stop here now, and continue in the next part of the series.
Related LinksECMAScript Basics
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
Scopes in ECMAScript
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
More Related Links
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module