JavaScript Statements
JavaScript Programs
A computer program is like a set of directions for a computer to follow.
In a computer language, these commands for programming are known as statements
.
A JavaScript program is a set of commands written using JavaScript.
In HTML, the web browser runs JavaScript programs.
JavaScript Statements
JavaScript code consists of:
Words, Symbols, Phrases, Specific Words, and Notes.
This instruction tells the web browser to display the words "Hello Dolly." within a specific part of a webpage identified by the unique name "demo."
Many JavaScript programs have lots of JavaScript statements.
The instructions are carried out step by step, exactly in the same order they are written.
JavaScript programs and statements are commonly referred to as JavaScript code.
Semicolons ;
In Javascript, semicolons are used to split different instructions or commands.
Put a semicolon at the end of every command that can be executed:
You can put several commands on one line if you separate them with semicolons.:
Online, you may come across instances where semicolons are missing. Using semicolons at the end of statements is not obligatory, but it's strongly advised.
JavaScript White Space
JavaScript doesn't pay attention to extra spaces. You can include extra spaces in your code to make it easier to read.
Here are some lines that mean the same thing.
let person = "Hege";
let person="Hege";
A helpful tip is to add spaces before and after operators like =, +, -, *, and / in your code.
let x = y + z;
JavaScript Line Length and Line Breaks
To make code easier to read, programmers usually prefer to keep each line of code shorter than 80 characters.
If a JavaScript instruction is too long to fit on one line, it's a good idea to split it after an operator:
JavaScript Code Blocks
You can put JavaScript statements together in code blocks by using curly braces {...}.
You can often see statements grouped together in blocks within JavaScript functions.
In this guide, we use two spaces to organize our code neatly. Later on, you'll get to know more about functions through this tutorial.
JavaScript Keywords
JavaScript commands usually begin with a special word to tell the computer what to do in JavaScript.
Our Reference for Special JavaScript Words contains a complete list of all the important words in JavaScript.
Here's a list of words you'll discover in this tutorial:
Keyword | Description |
---|---|
var | "Defines a variable." |
let | States the existence of a block variable. |
const | States that something remains unchanged as a block. |
if | Indicates a group of instructions to be carried out when a certain condition is met. |
switch | This code is like a signpost for a group of instructions to run under different conditions. |
for | Defines a group of commands to repeat in a loop. |
function | Defines a function. |
return | Leaves a function. |
try | Adds a way to deal with mistakes in a group of instructions. |
JavaScript keywords are special words that have a specific meaning in the language. You can't use these words as names for your variables.