JavaScript Program

A JavaScript program is a collection of programming statements which contain the instructions that need to be executed.

JavaScript Statements contain keywords, operators, values, expressions, and comments.

Use of Semicolons: Semicolons are used to separate the JavaScript statements.

Example:

P= 8;

Q = 10;

You can specify multiple statements in one line separated by semicolons.

P= 8; Q = 10;

JavaScript White Space: You can include white spaces to the JavaScript code to make it easily readable. However, JavaScript doesn’t acknowledge multiple spaces.

JavaScript Line Breaks: It is advised to break the JavaScript statements following an operator, if they fit properly in one line.

Example:

.lang-js
document.getElementById("demo").innerHTML =

"Introduction to JavaScript"

JavaScript Code Blocks: Always group the JavaScript statements in the curly brackets . The statements mentioned in the code block run together. Generally code blocks are found in JavaScript functions.

Example:

.lang-js
function myFunction() {

document.getElementById("demo1").innerHTML = "Introduction to JavaScript ";

document.getElementById("demo2").innerHTML = "Let’s learn";

}

JavaScript Keywords: Some of the most-commonly used JavaScript keywords are as follows:

| Var | Declares a variable | | --------------------------- | ----------------------------------------------------------------- | | Let | Declares a block variable | | Const | Declares a block constant | | If | Specifies a block of statements to be executed on a condition | | Switch | Specifies a block of statements to be executed in different cases | | For | Specifies a block of statements to be executed in a loop | | Function | Declares a function | | Return | Exits a function | | Try | Implements error handling to a block of statements |

JavaScript Print: JavaScript doesn’t offer print methods, thus you cannot access the output devices.

Use the window.print( ) to print the contents of the current window.

Example:

<!DOCTYPE html>

<html>

<body>

<button onclick="window.print()">Print this page</button>

</body>

</html>