JavaScript Basis: Syntax & Structure

JavaScript Basis: Syntax & Structure
by Janeth Kent Date: 07-02-2019 JavaScript manual tips

Programming languages are defined by rules. The syntax is what we follow when we write our code, which forms the logical structure of our programs.

Let's dive right into the basic components of JavaScript. We will look at values, unicode, semi-colon, indentation, white spacing, commentary, case sensitivity, keywords..ect.

By taking the time to learn the basics, we will be well on our way to building more functional and readable code.

JavaScript: the Values

There are two types of values in JavaScript: Fixed values( or literals) and variables.


Literals are defined as values written in our code, such as numbers, strings, booleans, as well as literals for objects and arrays.


18          // A number
'Toy'      // A string (can be in double "" or single '' quotes)
false        // A boolean (can be true or false)
['a', 'b']                           // An array
{color: 'red', shape: 'Circle'}     // An object


Variables are named values that store data. We declare our variables with the var, let or const keywords and assign values with the sign =.

For instance, the key is defined as a variable.

const key;
key = pluto;


Don’t use var. It should be used when you working with legacy code. It's the old pre-ES6 syntax.

Use let it if your variable needs to be updated within the program.

Use const it if your variable holds a constant value. It cannot be updated.


Camel Case

What if more than one word is our variable name? How do we declare a variable we need to name "first-item" for example?

We can't use hyphens such as "first-item" just because they are reserved for JavaScript subtractions.

Underscores such us first_name? We coul... but it has a tendency to make our code look messy and confusing.

What solution do we propose?

camel case! e.g. firstItem.

The first word is lower-case, the first letter of any subsequent words are upper-case. 



JavaScript uses a set of characters with unicode. Unicode covers almost all the characters, punctuations and symbols!. This is great because we can write our names in any language and even emojis can be used as variable names



JavaScript programs are built up with instructions known as statements. Such as:

let a = 500;
a = b + c;
const time =;

JavaScript statements often end in a semicolon ;. But semicolons aren’t always mandatory! JavaScript does not have any issues if you don’t use them.

// valid!
let a = 500
a = b + c
const time =


But again, there are some situations in which they are obligatory. For example, when we use a for loop, such as:

for (i = 0; i < .length; i++) { 

When using a block statement however, semicolons are not to be included after the curly braces, for example:

if (name == "Silvia") {
  // code
}                           // <- no ';'
function people(name) {
  // code
}                           // <- no ';'


If we’re using an object however, like:

const person = {
  firstName: "Silvia",
  lastName: "Brown",
  age: 30,
  eyeColor: "green"
};                          // the ';' is mandatory



Technically, we could write a whole program of JavaScript on one line. But itwould be almost impossible to read and maintain. That's why lines and indentation are used. The conditional statement can be used as an example:

if (loginSuccessful === 1) {
  // run code
} else {
  // abort

By applying indentation we’ll have a code cleaner, more maintainable and readable! Look:

if (loginAttempts < 5){
  if (loginAttempts < 3){
    alert("< 3");
  } else {
    alert("between 3 and 5");
} else {
  if (loginAttempts > 10){
    alert("> 10");
  } else {
    alert("between 5 and 10");


White Space

JavaScript requires only one space between keywords, names, and identifiers, or any white space will be completely ignored. This means that there is no difference in the language between the following statements:

const visitedCities="Rome, "+"Florence, "+"Venice";
const visitedCities = "Rome, " + "Florence, " + "Venice";

Don't tou think the second one's more readable?

Another example:

let x=1*y;       
let x = 1 * y;   

Again, the second line is much easier to read and debug!



A comment is code which can not be executed. They are useful for illustrating some code in a program. And also to' comment' on a code section to prevent execution- often used to test an alternative piece of code. JavaScript has two types of comments:

// Comment goes here
/* Comment goes here */

When writing code you may have some complex logic that is confusing, this is a perfect opportunity to include some comments in the code that will explain what is going on. Not only will this help you remember it later on, but if you someone else views your code, they will also be able to understand the code (hopefully)!



JavaScript Identifiers are names given to variables, functions, etc.

While naming your variables in JavaScript, keep the following rules in mind.

  1. You should not use any of the JavaScript reserved keywords as a variable name. These keywords are mentioned in the next section. For example, break or boolean variable names are not valid.
  2. JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or an underscore character. For example, 5demo is an invalid variable name but _5demo is a valid one.
  3. JavaScript variable names are case-sensitive. For example, Name and name are two different variables.


// Valid :)
// Invalid :(
[email protected]


Case Sensitivity

JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters. The while keyword, for example, must be typed “while”, not “While” or “WHILE”. Similarly, online, Online, OnLine, and ONLINE are four distinct variable names.

The following will throw an error:

function test() {
  alert("This is a test!");
function show_alert() {
  Test();                     // error! test(); is correct


Reserved Words

What reserved words are there in JavaScript?

 Reserved words of the JavaScript langauge are listed below. (Some of these words are actually used in the Java language, and are reserved in JavaScript for compatibility purposes or as possible extensions.) When choosing names for your JavaScript variables, avoid these reseved words!

break, do, instanceof, typeof, case, else, new, var, catch, finally, return, void, continue, for, switch, while, debugger, function, this, with, default, if, throw, delete, in, try, class, enum, extends, super, const, export, import.

See the full list of reserved keywords.


JavaScript Operators

Operators in JavaScript are very similar to operators that appear in other programming languages. The definition of an operator is a symbol that is used to perform an operation. Most often these operations are arithmetic (addition, subtraction, etc), but not always.

Arithmetical operators + - * and / are primarily used when performing calculations within JavaScript, such as

(2 + 2) * 100

The assignment operator = is used to assign values to our variables


JavaScript Expressions

Expressions are units of code that can be evaluated and resolve to a value. Expressions in JS can be divided in categories.;

  • Arithmetic expressions
  • String expressions
  • Primary expressions
  • Array and object initializers expressions
  • Logical expressions
  • Left-hand-side expressions
  • Property access expressions
  • Object creation expressions
  • Function definition expressions
  • Invocation expressions


Under this category go all expressions that evaluate to a number:

1 / 2 
i -= 2 

i * 2  


Expressions that evaluate to a string:

'A ' + 'string'  

'A ' += 'string'  


Under this category go variable references, literals and constants:






this //the current object  


i //where i is a variable or a constant  

but also some language keywords:



function* //the generator function 
yield //the generator pauser/resumer  

yield* //delegate to another generator or iterator  

async function* //async function expression  

await //async function pause/resume/wait for completion 

/pattern/i //regex  
() // grouping  


[] //array literal  

{} //object literal  


{a: 1, b: 2}  

{a: {b: 1}}  


Logical expressions make use of logical operators and resolve to a boolean value:

a && b  

a || b  



new //create an instance of a constructor 
super //calls the parent constructor  

...obj //expression using the spread operator  

PROPERTY ACCESS EXPRESSIONS //reference a property (or method) of an object 



new object()  
new a(1)   

new MyRectangle('name', 2, {a: 4})  


function() {}  
function(a, b) { return a * b }  
(a, b) => a * b  
a => a * 2   

() => { return 2 }  


The syntax for calling a function or method




This article was intended to provide an overview of JavaScript 's basic syntax and structure. We have looked at many of the common conventions, but remember that you can be somewhat flexible-especially when you work with your own specific standards in collaborative work environments.

Syntax and structuring play an important role both in the functionality of our programs and in the readability and maintenance of codes.

We hope you found this article useful!


by Janeth Kent Date: 07-02-2019 JavaScript manual tips hits : 4367  
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN END DEVELOPMENT.


Related Posts

Strings in JavaScript: What they are and how to use them

In this tutorial we are going to explain what strings are and how they are used in JavaScript. The tutorial is intended for people who are learning to program in…

Dates in local format with Javascript

In the articles we have about dates in JavaScript we were missing one about how to create dates in local format with JavaScript. That is to say, being able to…

Formatting hours in Javascript

Continuing with the set of articles that talk about internationalisation elements, like the previous one where we talked about relative dates in JavaScript, we will see in this one how…

Request data with prompt in JavaScript

After having published several articles about how to manipulate arrays and dates, today I will publish a post that some of you will find too basic and others will find…

Relative dates in JavaScript

One of the interesting things about the internationalisation library represented in the Int object is the handling of relative dates in Javascript. This handling allows us to represent a date…

How to access webcam and grab an image using HTML5 and Javascript

We often use webcams to broadcast video in real time via our computer. This broadcast can be viewed, saved and even shared via the Internet. As a rule, we need…

The JavaScript forEach loop

We have already talked about how to handle some of loops  types in Javascript including for, for-in and for-off loops. In the case of today we are going to see how…

What are React Hooks and what problems they solve

Working with React, - and before the release of Hooks in version 16.8 -  there was always the possibility to create components in three different ways based on a number of…

Flattening arrays in JavaScript

When we are handling arrays that are arrays or have multiple dimensions it can be very useful to know how to flatten arrays in JavaScript. That is to say, to…

How to populate an array with random numbers in JavaScript

Some of you might think that what we explained in the article on how to populate an array with numbers, apart from the didactic part, would not have much applicability…

How to populate an array with numbers in JavaScript

Populate an array with numbers in JavaScript The first step is to initialise the array. So today we are going to see a simple way to do it and see how…

Top Javascript Libraries and Frameworks Part 2

What are JavaScript frameworks?   JavaScript frameworks are application frameworks that allow developers to manipulate code to meet their particular needs. Web application development is like building a house. You have the option…

We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration