In this article, you will learn about the basics of Javascript and how to use them. The purpose of this article is to teach the bare essentials - just enough to get started; for a full understanding of Javascript, consider opting for our Full Stack Development Learning Path where we teach Javascript from basics to advanced with frontend as well as backend frameworks, databases, developer tools and deployment tools to make you an industry-ready full stack developer.

JavaScript is one of the most popular programming languages in the world right now. It is now widely being used outside of the browser from server-side to desktop and mobile applications. Javascript is a high-level, dynamically typed interpreted programming language.

"In case you’re wondering, JavaScript has nothing to do with Java, it’s an unfortunate name choice but we have to live with it."

Variables and Data Types

Variables are used to store data in a program. Variable has a name and a value. There are two parts of creating a variable; declaration and initialization. Once it’s created, you can set its value.

In Javascript, a variable is declared using the var keyword. (In ES6, you can use const and let to declare block-scoped variables)

A variable in JavaScript can contain any data. A variable can at one moment be a string and at another be a number. Programming languages that allow such things are called “dynamically typed”, meaning that there are data types, but variables are not bound to any of them.

Javascript has 7 basic data types:

1. Number

The number type represents both integer and floating-point numbers. You can perform *, /, +, - etc operations on numbers. Besides regular numbers, there are special numeric values like Infinity, -Infinity, and NaN (Not a number).

2. String

A string in Javascript can be surrounded by single quotes, double quotes or backticks. Backticks allow you to embed variables inside a string;

3. Boolean

A boolean has only two values: true or false. It is commonly used to store yes/no value.

4. Null

A special value that represents “nothing”, “empty” or “unknown value”.

5. Undefined

A special value which means ‘value is not assigned’.

6. Objects

All other types are called “primitive” because their values can contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store collections of data and more complex entities.

7. Arrays

An array is a single object that contains multiple values enclosed in square brackets and separated by commas.

Math and  Logic

A really important part of programming is doing math and being able to compare values in order to make decisions in code. The outcome can be either true or false (boolean type).

Conditionals

Conditionals are used to run a piece of code or another depending on the outcome of the comparisons made. Javascript provides if and if-else statements to run code based on conditions. You can also chain if-else-if to run code based on multiple conditions. Logical operators like && (AND), || (OR) and ! (NOT) can also be used for making decisions.

Looping

Loops allow you to run a piece of code a certain number of times or until a certain condition is met. They’re incredibly useful. They can be used to carry out actions on every item in an array or printing all the data inside an array or in searching etc.

Two of the most common loops in Javascript are for and while.

  • A for loop contains three statements, separated by a semicolon (initialization, condition, updating or final expression). The loop first runs the initialization and then checks if the condition is true. If it is true, it runs the block of code inside and finally runs the updation code. After updating, it again checks the condition and performs these steps again and again until the condition becomes false.
  • A while loop repeats a block of code until a condition is met. For any update, you can do it inside the while loop.

Functions

Functions are blocks of code that you can reuse and call anytime you want to execute it. A function can take values, called arguments, and can return a value.

Objects

A Javascript object is a collection of properties and methods/functions. It is a key-value pair collection where the value can be data or function. You can access the keys inside an object using the dot syntax. Objects are used to store relative information.

Arrays

Arrays in javascript are used to store a list of any kind of data. Each item in an array has an index/position (a number that can be used to retrieve an element from it). Arrays in Javascript start at 0; so the last element has an index one less than the length of an array. Arrays, by default also have some properties/functions assigned to them, which gives more information about the array like the length etc.


These are some of the basics of Javascript. Again, this is not a full guide to Javascript. It only covers certain things to get you started with Javascript. To get full in-depth knowledge, go through this documentation (Javascript | MDN) on Mozilla Developer Networks.

If you want to learn these concepts and much more from experts, consider enrolling in our Full Stack Development Learning Path. We teach frontend, backend, databases, dev tools and deployment tools from basics to advanced.