JavaScript:~ basics

This article covers some of the basics of JavaScript which will help you to get into some advanced and exciting stuff related to the world of web programming.

To begin with: we will talk about key elements that every programming language has, and how JavaScript incorporates each one of them. We will also discuss the basics of Object Oriented Programming in JavaScript.

Let’s jumpstart…

If we talk about modern day programming languages, they all have few things in common:

  1. Variables
  2. Constants
  3. Functions or Methods
  4. Classes
  5. Keywords
  6. Operators

Variables are named containers that store some data or reference.

Constants are the values that never change.

Functions or Methods are blocks of code that perform a certain set of tasks.

Classes are the blocks of code that contain methods and variables. These method and variables gets declared are a part of objects that refer to that class.

Keywords are the unique words that are used for the perpose of declaration and initialization.

Operators are the mathematical signs used for the purpose of performing and arithmetic or logical operation.

JavaScript variables are declared using keywords var or let. There is a difference between these two keywords which we will understand later in the tutorial: ES2015 features.

Example:

var myName = "Sachin";

let yourName = "Saurav";

The syntax for declaring and initializing a JavaScript variable starts with the keyword var or let, followed by variable name, the = operator which initializes the variable with value, and a valid value.

The = and value part can be omitted if we wish to leave the variable undefined. Such variables can be defined later when needed.

A variable once declared does not require to be declared again. Hence, the keywords var and let are used once in the lifecycle of variable.

Whenever a JavaScript variable is defined, it is always scoped. By scoping we mean, where the variable is accessible and where it is not. For instance,

If we define a variable within a function, it is scoped locally. It means that the variable’s lifecyle ends once the function is executed. The variable becomes inaccessible outside that function. Example:

function myFunc () {
     var name = "John";
}
name = "Johny"; // Oops! Variable "name" not accessible here...

FYI: This is how we define functions in JavaScript. The syntax starts with keyword function, followed by function name, open and close parenthesis for accepting external parameters, and a block of statements started with a curly brace and ending with a curly brace.

Unlike other programming languages, functions in JavaScript can be anonymous. In other words, we can omit the function name from syntax. However, having anonymous function doesn’t make any sense since it cannot be called. Fortunately, JavaScript treat functions as first class objects. We can assign them variables like other objects. It brings us to another way of defining functions:

var myFunc = function () { 
     ... 
};

Here: the variable name works like a function name.

To call a function we simply use the function name followed by the parenthesis.

myFunc();

As mentioned earlier, we can pass data to a function as external parameters. This is how we do it:

myFunc("John"); // Passing the name John to the function myFunc

And this is how we accept parameters in function definition:

function myFunc (name) {
     ...
}

The parameter “name” is a JavaScript variable. When used within parenthesis, it does not require declaration using var keyword.

We can pass multiple parameters as comma separated values:

function myFunc (name, surname) { ... }

myFunc("John", "Cena");

That’s enough about functions for now. Let’s talk about constants in JavaScript.

Constants were never supported by JavaScript in previous versions. However, with recent advancements and implementation of new ES2015 features, JavaScript allows you to define constants.

Constants are the values that cannot be altered once defined. In JavaScript we define constants as follows:

const PI = 3.14;

The keyword const is supported only in latest browsers. So does the keyword let. Hence, we do not advice you to use them in production code. These new features are covered in details in the topic ES2015.

As mentioned, constant values can never be altered once defined. Hence, if we try to do something like this:

const PI = 3.14;
PI = 22/7; // Error!

it will result into an error.

Continued in Part 2 >>

Advertisements

1 thought on “JavaScript:~ basics

  1. Pingback: JavaScript:~ the start | Speed JS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s