Syntax Basics


Operators, Control Structures

Abel Sanchez and John R. Williams

Variables

Reserved Words

JavaScript has a list of reserved identifiers. You cannot use these words for your identifiers - click on image to expand

Words You cannot use

Identifiers

An identifier must start with a letter, a dollar sign, or an underscore. After the first character, you may use letters, numbers, underscores, or dollar signs.

Identifiers

Can you find the error?

Case Sensitive

Case Sensitive

Literals

A literal is a value that you directly represent in your program - as opposed to a variable.

Whitespace

Use whitespaces to format your code and improve readability.

vs

Whitespaces are ignored in JavaScript.

Loading JavaScript Files

Loading JavaScript Files

Comments

Comments: Single Line & Multi Line

For single line, everything after // is ignored. In multi line, everything between /* and */ is ignored.

Comments

Can you find the error?

HTML Comments

Comments

Line Terminators

JavaScript uses a semicolon to separate statements.

Automatic Semicolon Insertion

If you forget a semicolon, JavaScript will try to add a semicolon for you. This can be very dangerous. Always finish your statements with a semicolon.

Active Learning

Active Learning

Code output?

Line Break

For the return keyword, the break keyword, the continue keyword, and the throw keyword, a line break will be interpreted as a semicolon.

If ... Else

If ... Else

Comparison Operators

Take x=7 for the following comparisons.

Operator Description Comparing Returns
=== equal value/type x === 7 true
!== not eq value or type x !== 7 false
> greater than x > 7 false
< less than x < 7 false
>= greater or equal x >= 7 true
<= less or equal x <= 7 true

Comparison Operators

Take x=7 and y=5 for the following comparisons.

Operator Description Comparing Returns
&& and (x < 10 && y > 1) true
|| or (x === 7 || y === 7) true
! not !(x === 7) false

Active Learning

Download images & starter code


click on zip image above

For Loops

Loops - Active Learning

For Loops

Lexical Conventions

Style Guide (reference)

JavaScript Reference

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference

THE END