Introduction to JavaScript: Basics

 JavaScript is the programming language that lets the Internet work. The Internet would be nothing without JavaScript and in this lesson, you will find out why. 




At the end of this article, you should be able to:

  • understand what Javascript is and explain its use in web development.
  • explain and use JavaScript primitive data types and variables.
  • explain and use JavaScript functions as properties and methods on primitive data types.
  • explain global object in JavaScript and be able to use the Math object.
  • explain basic control flow and if/else statements.

Learn

Learn to understand what Javascript is and explain its use in web development.

Overview

JavaScript is the third of the major building blocks of a web page. Without it, we wouldn’t have the dynamic content and usability we expect from modern websites. We will now learn what it is (and isn’t) and how it is used, not only on the web but in all of its applications.

What is JavaScript and why do we use it?

JavaScript is a programming language that was first created in 1994 as a way to add functionality and user interaction to a website. If we think back to our analogy of a web page as a house (Introduction to Web Development Fundamentals), we will remember that we said that JavaScript is the electricity, plumbing, and gas. It is what makes the web page “run”. JavaScript was originally designed to be used purely on the front end as a way for web developers to add functionality to their web pages, and in its early days, it did just that. Recently, the introduction of the “V8 engine” by Google has improved the speed and functionality of JS. That led to the development and release of exciting new front end JavaScript frameworks and eventually Node.js, a way to run JavaScript on a server (back end). This new development has led to a resurgence of JavaScript. Now, JavaScript is one of the world’s most widely-used programming languages. We can find JavaScript used on the front end, back end, mobile, Internet of Things (Iot), game development, and really anywhere a traditional programming language would be used. Recently, the newest version of the JavaScript language was released, ES6*.

JavaScript vs Java (and other languages)

Keep in mind, JavaScript != Java. Although they share similar names (this was, unfortunately, considered a feature by JavaScript’s early pioneers) that is where the similarities end.

How to ‘run’ JavaScript

JavaScript, being the de-facto language of the Internet, is usually run from within an Internet browser. In fact, you can write all of the JavaScript you want and watch it run in real-time right in your browser by pressing F12 (for Windows), or Cmd+option+J (for Mac) (for Google Chrome). This will open up your console (we will learn more about the console later).

Learn

Learn to explain and use JavaScript primitive data types and variables.

Overview

In order to understand the JavaScript language, the first step is to understand and be able to use variables and primitive data types.

Variables

At the heart of JavaScript are variables. A variable is a way to store the value of something to use later. (A note for those with previous programming knowledge: JavaScript is a loosely-typed language, which means that a variable can be set (and reset) to any type. We do not need to declare its type when initiating the variable.)

var

var is the ES5 way of declaring a variable. This is a generic variable keyword.

let

let is a new ES6 variable keyword. This will assign a variable much like var, but with a little bit different behavior. Most notably, it differs by creating “block level scope”.

const

const is also new in ES6. A const variable is a variable that cannot be changed. It’s short for “constant”.

Primitive Data Types (String, Number, Boolean)

The term ‘primitive data type’ refers to the fact that these are the most basic data types in the language. All other data types (which we will learn about in later lessons) use these types.

Strings

Strings are blocks of text. They will always be defined with quotation marks around them, either single or double. Any text with quotes around it is a string.


1. const name = 'atharv';


Numbers

Numbers are just that, numbers. Numbers do NOT have quotes around them. They can be negative as well. JavaScript does have a limitation on the size of a number (+/- 9007199254740991), but only very rarely will that limitation come up.


1.  const answer = 42;

2. const negative = -13;


Booleans

Booleans come from low-level computer science. It is a concept that powers binary code and the very core of computers. You may have seen binary code in the past (e.g., 0001 0110…). That is Boolean logic. It essentially means you have two choices, on or off, 0 or 1, true or false. In JavaScript, we use Booleans to mean true or false. This may seem simple at first but can get complicated later on.


1.  const iLoveJavascript = true;


Math Operators

One of the first jobs a computer had was to compute numbers. In JavaScript, we have built-in math operators that work exactly as they do on your calculator.

+ — * / =


1+1 = 2;

2*2 = 4;

2-2 = 0;

2/2  = 1;

%

Something you may not have seen before is the Modulo (%). This math operator will divide the two numbers and return the remainder.


21%5 = 1;

21%6 = 3;

21%7 = 0;

Learn

Learn to explain and use JavaScript functions as properties and methods on primitive data types.

Overview

As we progress through our introduction to JavaScript, we will learn the types of things we can do with those primitive data types. In this objective, we will learn about functions.

Properties and Methods

Primitive data types (and other data types) have built-in functionality known as properties and methods. These extend the functionality of the primitive data types and allow us to gather information about them, or manipulate them in some way. Both properties and methods will be accessed using the dot notation where we give the name of the variable, a dot, then the name of the property or method.


1.  variableName.propertyname;


Properties

Properties allow us to access data from a data type. There are many different properties on every data type that will give you a bit of information about that specific object.

Methods

Methods allow us to manipulate a data type. Methods are different from properties in that they need to have parentheses on the end.

Functions

Functions allow us to perform many computations and return a final product. When we run a computer program, we are running a series of functions, and reading or manipulating what they return. You may not have realized this, but we have already worked with a type of function: a method.

Anatomy of a Function



1.  function my func() {}


A function will start with the function keyword. This tells whatever is running your program that what follows is a function and to treat it as such. After that comes the name of the function. We like to give functions names that describe what they do. Then comes open and close parentheses. And finally, open and close brackets. In between these brackets is where all of our function code will go.

1.  function logsHello() {
2.  console.log('hello');
3.  }
4.  logsHello();

In this example, we declare a function logsHello and we set it up to console.log ‘hello’. We can then see that in order to run this function, we need to write its name followed by parentheses. This is the syntax to run a function. A function always needs parentheses to run.

Arguments

Now that we can run a basic function, we are going to start passing it arguments.


𝐋𝐢𝐤𝐞

𝐒𝐡𝐚𝐫𝐞

Tags