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 : 3812  
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

Validating HTML forms using BULMA and vanilla JavaScript

Today we are going to write about contact forms and how to validate them using JavaScript. The contact form seems to be one of the top features of every basic home…

A FULFILLED PROMISE - Using the FETCH API to make AJAX calls

In this article we talked about what AJAX calls are and how to use them in a traditional way, by using the XMLHttpRequest (XHR) object. In short, thanks to AJAX…

How to use Parallax.js effect on your website

Today, we're going to write about the parallax effect, similar to parallax scrolling, and how to implement it to improve your landing page. In webdev, they say mobile first -…

How to make the website's dark mode persistent with Local Storage, CSS and JS

Recently we wrote about how to do a switchable alternative color mode or theme, a very useful and popular feature to websites. Today’s article is going to be about how…

Dark Mode on website using CSS and JavaScript

In today’s article we are going to learn how to build pretty much standard these days on the web pages and that is the alternative color mode and switching between…

JavaScript: Spread and Rest operators

In today’s article we are going to talk about one of the features of the ES6 version(ECMAScript 2015) of JavaScript which is Spread operator as well as Rest operator. These features…

Javascript: what are callbacks and how to use them.

Today we are going to learn about a concept that is widely used in javascript and that is used quite a lot by today's frameworks, libraries, especially NodeJS. This is…

HTTP Cookies: how they work and how to use them

Today we are going to write about the way to store data in a browser, why websites use cookies and how they work in detail. Continue reading to find out how…

The package managers npm and yarn: main differences

Npm and yarn are package managers that help to manage a project’s dependencies. A dependency is, as it sounds, something that a project depends on, a piece of code that…

The Javascript asign() method to merge and clone objects

In this article, we will be covering Object.assign()method in javascript in detail with examples. A javascript object is a collection of key-value pairs. Keys are also known as properties of object. Keys…

All the javascript functions and methods to manipulate arrays

This article will show that the prominent JavaScript array functions are .map(), .filter(), and .reduce(), and will then go through examples of instances in which .every() and .some() would save…

TypeScript: The evolution of JavaScript

When you're involved in the development of a large project, programming languages like JavaScript don't seem to be the best solution. Their lack of elements such as Language Aids has…

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