By Shubhra Sarker
Date: July 26, 2019
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.
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.
Most of the time, JavaScript needs to work with information. Consider two scenarios-
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.
There are two limitations on variable declaration javascript variable-
Examples of some valid variables name in JavaScript-
Examples of some invalid variables-
Variable named message and Message are two different variables.
Constant variable means unchangeable. To declare constant variable, we use const keyword in JavaScript.
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.
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-
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 in JavaScript must be surrounded by quotes.
In JavaScript, there are three types of quotes-
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:
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.
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 meaning of undefined is “value is not assigned”.
If a variable is declared, but not assigned,its value is undefined:
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 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 :
The call to type of variable returns a string with the type name.
We will discuss more about the Fundamentals of JavaScript like operators, conditions and other things in my next blogs.
Want to receive a fortnightly round up of the latest tech updates? Subscribe to
our free newsletter. No spam, just insightful content covering design,
development, AI and much more.