Learn about Numbers and Operators in Javascript

NUMBERS AND OPERATORS

Github Code – https://github.com/ankur-srivastava/javascript-lectures/blob/master/Lecture3/index.js

Usually we deal with three types of numbers –

  1. Integers like 3, 34
  2. Floating Point like 3.12, 93.34
  3. Doubles like 1.3452345

JS has only one data type for numbers called Number.

Let’s declare two numbers

let num1 = 32;
let num2 = 44;

Arithmetic Operators

With JS you have all the mathematical operators available to perform various operations.
Like add, substract etc.

2+3
4/2
4%2

We use operator precedence rules when evaluating a complex expression

let x = 3+2*6/2

It’s good to use parenthesis.

(3+2)*(6/2)

Increment Operators using Postfix operator

let x = 4;
x++
x

Comparison Operators

If you want to check that two values are identical you can use === operator
Similarly to check if they are not equal you can use !==

=== compares the data type also, in addition to it’s value. == does not.

Other frequently used comparison operators include : <, >, <=

Mostly in conditional statements like if-else
if(x > y)

Learn about Variables in Javascript

If you have done any programming you will be at home with variables. If you haven’t then don’t worry it’s quite easy to understand.

Variables are used to store values. Value can be a string, a number or other forms of data.

Code - https://github.com/ankur-srivastava/javascript-lectures/blob/master/Lecture2/index.js

Let’s understand this with an example. You may have seen websites that ask you to enter your first name etc. Now to store that name we need a variable.

let firstName = 'Ankur';

Here firstName is a container that stores Ankur.

Declare a Variable
To use variables you need to declare them. We use keywords like let, var or const to declare variables. const is used to declare variables whose values do not change.

let age = 30;
const color = 'red';

What happens if you just write

let day;
console.log(day)

It gives undefined, since we dint assign any value.

Initialize a Variable
Initialize means assigning a value to a variable.

let day = 'Tuesday';
console.log(day)

Why you should use let instead of var ?

With var you can declare a variable after initializing. This is known as hoisting. And is not good. You cannot do the same thing with let.
Another thing that works with var and not let is that with var you can declare a variable multiple times.

var fName = 'Ankur';
var fName = 'Sid';

For these reasons it’s not advisable to use var.

A variable declared using let or var can be updated.

let name = 'Jack';
name = 'Sid';

Variable Naming Rules

1. Don’t use numbers at the start of variables. This is not allowed.
2. Variable names are case sensitive.
3. Using camelCase is safe and recommended.
4. Variable names should be intuitive.
5. Don’t use the reserved words. That is words that are part of JS syntax. Like let, const, function etc.

Variable Types

You don’t need to specify variable types in JS. It’s a dynamically typed language, quite similar to Python. In other languages like C or Java you may have seen various data types being used like int, long, boolean, float etc. Not needed with JS.

You can store numbers, strings, booleans, arrays and objects.

Let’s see some examples:

let message = 'Hello all';
let age=32;
let flag = false;
let a = [10, 12, 32]

An introduction to Javascript

How Javascript was born ?

In 1993 the first web browser with a GUI was released. It was called Mosaic. Due to it’s GUI it made www quite popular.

It’s lead developers then founded Netscape which released an enhanced version called Netscape Navigator. The pages were static though. They lacked interaction.

To make the pages more interactive Netscape needed a scripting language. To do this they hired Brendan Eich. Same guy who later co-founded Mozilla project. His job was to put this language “in the browser”. The first version was completed in 10 days. It was called Mocha. Then renamed to Livescript and subsequently to Javascript.

What exactly is Javascript ?

Javascript is an interpreted or just in time compiled programming language. What this means is that the program you write is compiled at run time.
Unlike Java where you need to first compile the program and then run the generated byte code.

It is one of the core technologies of the World Wide Web (www) along with HTML and CSS. You can build interactive websites using JS. When you see some animations like page elements fading in and out or resizing then it’s more or less the work of JS.

All major web browsers have a dedicated JS engine which can execute the JS code.

Most of the websites you see use Javascript to handle client side behavior. But with the advent of technologies like Node JS we can now write code in JS which can be executed on Server side.

Why should you learn Javascript ?

To build any modern day website Javascript is required. And now JS can be used to build backend, like APIs. So whether you want to work for a company as a developer or you plan to build your pwn websites you will need JS.

Code – https://github.com/ankur-srivastava/javascript-lectures/tree/master/Lecture1

PPT – Introduction to JavaScript from Ankur Srivastava