JavaScript - Statements

JavaScript Statements

JavaScript statement is made up of values, operators, expressions, keywords and comments.

The statement above instructs the browser to write Hello Daniel inside the HTML element that has the id attribute value of para1.

tips
TIP:

JS statements are executed one after another in the same order as they were written.


Semi colons

This is used to separate JavaScript statements.

White Space

JavaScript ignores white spaces, but it is good for you to add white space in order to make your code easy and cool to read.

Case Sensitivity

JavaScript is case sensitive; that is, getElementById() is NOT same as getelementByID().

Line Length and Breaks

Code lines longer that 80 characters should be avoided for best readability. Also, line breaks are best done after an operator.

Grouping/Code Blocks

JavaScript statements can be grouped together inside curly brackets { }. It is usually found in JavaScript functions when you want the statement to be executed together.

Keywords

They are used to identify JavaScript action to be performed. Some keywords and description include:

KEYWORDS DESCRIPTION
var

Declares a variable.

let

Declares a block variable.

const

Declares a block constant.

if

Tells a block of statement to execute based on a condition.

switch

Tells a block of statement to execute based on different conditions (or cases).

for

Tells a block of statement to execute in a loop

function

Declares a function

return

Exits a function

try

For error handling of a block statement.


note
NOTE:

Keywords cannot be used as variable names.


Variable Cases

JavaScript variables can be written in Pascal Case, Lower Camel Case, with an underscore.

  • Pascal Case - SmartKid, DanielMary
  • Lower Camel Case - smartKid, danielMary
  • Underscore - smart_kid, daniel_mary
note
NOTE:

JavaScript does not allow hyphen ( - ) in variable cases.


Comments

JavaScript use the following for comments:

  • //
  • /* */
  • </-- //-->

Values

There are two (2) major type of values in JavaScript: Fixed and Variable values.

  • Fixed Values (Literals)
    • Numbers - written with or without decimals
    • Strings - text with double or single quotation.
  • Variable Values - They are used to store data.
    Keywords: var, let and const are used to first declare the variable, the followed by an equal to sign; to assign the value to the variable.

Operators

They are used for arithmetic operators. They include:

  • +
  • -
  • *
  • /

Expression

This is the combination of values, variables and operators to display an output.

Identifiers/Names

They are JavaScript names used to name variables, keywords and functions. It follows these rules:

  • Name must begin with a letter (A-Z or a-z), dollar sign ($) or an underscore (_)
  • subsequent characters can be letters, digits, underscore or dollar signs
  • Numbers are not allowed to be used as the first character.
ADVERTISEMENTS

LEARNING IS A CONTINOUS PROCESS - PRACTICE MAKES PERFECT