JavaScript Statements Flashcards

JavaScript Programs A computer program is a list of "instructions" to be "executed" by a computer. In a programming language, these programming instructions are called statements. A JavaScript program is a list of programming statements.

1
Q

p>A JavaScript program is a list of statements to be executed by a computer./p>

A
script>
var x, y, z;  // Statement 1
x = 5;    // Statement 2
y = 6;    // Statement 3
z = x + y;  // Statement 4

document.getElementById(“demo”).innerHTML =
“The value of z is “ + z + “.”;
/script>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

JavaScript Statements
JavaScript statements are composed of:

Values, Operators, Expressions, Keywords, and Comments.

This statement tells the browser to write “Hello Dolly.” inside an HTML element with id=”demo”:

A

script>
document.getElementById(“demo”).innerHTML = “Hello Dolly.”;
/script>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Semicolons ;
Semicolons separate JavaScript statements.

Add a semicolon at the end of each executable statement:

A
script>
var a, b, c;
a = 5;
b = 6;
c = a + b;
document.getElementById("demo1").innerHTML = c;
/script>

When separated by semicolons, multiple statements on one line are allowed:

script>
var a, b, c;
a = 5; b = 6; c = a + b;
document.getElementById(“demo1”).innerHTML = c;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

JavaScript White Space
JavaScript ignores multiple spaces. You can add white space to your script to make it more readable.

The following lines are equivalent:

A
The following lines are equivalent:
var person = "Hege";
var person="Hege";

A good practice is to put spaces around operators ( = + - * / ): var x = y + z;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

JavaScript Line Length and Line Breaks
For best readability, programmers often like to avoid code lines longer than 80 characters.

If a JavaScript statement does not fit on one line, the best place to break it is after an operator:

The best place to break a code line is after an operator or a comma.

A

script>
document.getElementById(“demo”).innerHTML =
“Hello Dolly!”;
/script>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

JavaScript Code Blocks
JavaScript statements can be grouped together in code blocks, inside curly brackets {…}.

The purpose of code blocks is to define statements to be executed together.

One place you will find statements grouped together in blocks, is in JavaScript functions:

JavaScript code blocks are written between { and }

A

button type=”button” onclick=”myFunction()”>Click Me!/button>

script>
function myFunction() {
document.getElementById(“demo1”).innerHTML = “Hello Dolly!”;
document.getElementById(“demo2”).innerHTML = “How are you?”;
}
/script>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

JavaScript Keywords
JavaScript statements often start with a keyword to identify the JavaScript action to be performed.

Here is a list of some of the keywords you will learn about in this tutorial:

JavaScript keywords are reserved words. Reserved words cannot be used as names for variables.

A

break Terminates a switch or a loop
continue Jumps out of a loop and starts at the top

debugger Stops the execution of JavaScript, and calls (if available) the debugging function

do … while Executes a block of statements, and repeats the block, while a condition is true

for Marks a block of statements to be executed, as long as a condition is true

function Declares a function

if … else Marks a block of statements to be executed, depending on a condition

return Exits a function

switch Marks a block of statements to be executed, depending on different cases

try … catch Implements error handling to a block of statements

var Declares a variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly