July 26, 2019 - 3:19 pm

The Fundamentals of JavaScript

In this blog, we are going to discuss the fundamentals of JavaScript like variables, data types, conditions, looping, and other topics. 

The previous blog was about Introduction to JavaScript where we discussed on JavaScript history why it is famous among all web developers, why should we use it in modern web development, and it’s future. 

Run First Script 

JavaScript codes can be inserted into any part of HTML documents (I hope you have enough knowledge of HTML & CSS) with the help of script tag. For example  

Code inside <script> tag automatically executed when the browser renders the page.

<script> tag has few attributes that are rarely used nowdays.

The type attribute : <script type = “text/javascript”>

External Script: If we have a lot of JavaScript code, we can put it into a separate JavaScript file. JavaScript file attach to HTML using <src> attribute.

Variables 

Most of the time, JavaScript needs to work with information. Consider two scenarios- 

  1. In an online shop, we store different kinds of data like sold goods, pricing etc. 
  2. In a chat application, the data might include users, messages, and much more

Variables which are used to store information.

To create a JavaScript variable, use the let keyword. 

The statement below creates (in other words: declares or defines) a variable with the name “message”:

Now we can put some value in message variable using assignment operators =,


var instead of let

The var keyword is almost the same as let. It also declares a variable, but in a slightly different. We will discuss briefly about that topic our next blog.

Variable naming

There are two limitations on variable declaration javascript variable-  

  1. The name must contain only character, number and digit and symbol
  2. The first character must not be a digit

Examples of some valid variables name in JavaScript- 

Examples of some invalid variables- 


Case matters

Variable named message and Message are two different variables.

Constants

Constant variable means unchangeable. To declare constant variable, we use const keyword in JavaScript.


Name Things right 

Talking about variables, there’s one more extremely important thing.

A variable name should have clean, obvious meaning, as it describes the data that it stores.

Variable naming is one of the most important and complex skills in programming. So a quick glance at variable names can reveal which code was written by a beginner versus an experienced developer.

Data Types

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.

There are seven data types in JavaScript-

A Number 

The number type represent both integer and floating point number.

There are many operation for number data type, for example multiplication “*” ,division “/” , Addition “+”, substract “-” and so on.

A String 

A string in JavaScript must be surrounded by quotes.

In JavaScript, there are three types of quotes- 

  1. Double quote : “Hello”.
  2. Single quote : ‘Hello’.
  3. Backtics : `Hello`.

Double and single quotes are same. Because there is no difference between them in JavaScript.

Back-ticks are “Extended Functionality” quotes. They allow us to embed variables and expressions into a string by wrapping them in ${…}, 

for example:


A boolean (Logical type)

The boolean type has only two types- true and false.

This type commonly used to store yes/no value. True means yes and false means no.


A Null 

Null type just a special value which represents “nothing”, “empty” or “value unknown”.

The code above states that age is unknown or empty for some reason.

The undefined value 

The meaning of undefined is “value is not assigned”.

If a variable is declared, but not assigned,its value is undefined:


Objects and Symbols 

All other types are called “Primitive” because their values can contain only a single thing. In contrast, objects are used to store collections of data and more complex entities.

The symbol type is used to create unique identifiers for objects.

The type of operator 

The type of operator returns the type of the argument. It’s useful when we want to process values of different types differently or just want to do a quick check.

There are two types of syntax : 

  1. As an operator : typeof a
  2. As a function : typeof(a)

The call to type of variable  returns a string with the type name.

We will discuss more about JavaScript fundamentals like operators, conditions and other things in my next blogs. 

Share this post

Leave a Reply

Your email address will not be published.