Introduction to JavaScript Syntax (Part 1)

Introduction to JavaScript Syntax (Part 1)

Hi there,

In this article, the only assumption I will make, is that you are a beginner looking to learn JavaScript either frontend web development or backend (using node.js).

Either ways, you need to understand JavaScript as a programming language and how to write it correctly.

This will be part of a 4 series tutorial to help you understand the nitty-gritty of the topic. Here, we will discuss Variables, Data types, and Arithmetic Operations

Let's begin!

Variables

As with all other programming languages, JavaScript also has the concept of variables. These are what we use to 'hold' a value in memory and refer back to it later in our program to perform some other operations.

For a short example:

let thisVariable = "this value"

The let keyword above in-built into JavaScript as a way to tell the JavaScript (JS) engine that will run the code that we want to declare a variable called thisVariable and the = sign tells the JS engine again that the value we want the variable to hold is "this value".

Another example:

let myBestFood = "rice"

There are other keywords that are used to define or declare variables, we also have const and var. Their purposes are differ.

The const keyword is used to define or declare variables whose value cannot be changed.

The var keyword is used to declare variables whose value can be accessed or changed from anywhere in your code.

examples:

const myHeight = 181 //this variable cannot be reassigned another value
var myBagPackColor = "green" //this variable's value can be accessed anywhere in the program

Note: Variables cannot have space!

Data types

In JavaScript, there are different data types. The values that we are 'holding' in variables above have different types. they can be Strings, Numbers, etc.

Notice the value of the variable below:

let myBagPackColor = "green" // this value is a string

"green" in quotes("") is a string data type.

123 is a number data type.

true or false are regarded as Boolean data types. They are the only kind of data that can be called Booleans.

There is also undefined which is an absence or data and type. And there is null which is what is used to denote that there is no value. They can also be assigned to variables. example:

let unknownVariable = undefined

var something = null

All the above mentioned data types are known as Primitive Data types. That is, String, Number, Boolean, Undefined, and Null.

Arithmetic Operations

You can do some mathematics in JavaScript too! When you need to add up some data directly from your code, JavaScript has some allowance.

Addition

let firstNumber = 12

let secondNumber = 20

let result = firstNumber + secondNumber //This will give 32

Pretty straight, right?

Subtraction

let firstNumber = 12

let secondNumber = 20

let result = secondNumber - firstNumber // This will give 8

Same goes for multiplication using (*), division using (/) and also modulus (remainder), using (%).

We've covered quite a bit here!

Quick recap:

Variables help us hold values in memory. This values can have different data types some can be string, number, booleans or even null and undefined. The number types can be used to perform arithmetic operations.