Javascript is a programming language of the web. It was invented by Brendan Eich in 1995.
Why learn Javascript
- It's the most popular programming language.Javascript helps you develop great front-end as well as back-end softwares using different Javascript based frameworks like jQuery, Node.JS etc.
- JavaScript is everywhere.It comes installed on every modern web browser and so to learn Javascript you really do not need any special environment setup.
- It's a valuable tool for game designers. Games are a big business today, and developers who know JavaScript have that extra advantage. The language's versatility, power, and ability to easily create visual effects make it a perfect fit for game developers.
- Javascript helps you create really beautiful and crazy fast websites. You can develop your website with a console like look and feel and give your users the best Graphical User Experience
- JavaScript is easy to learn
Javascript Frameworks
Thera are many javascript frameworks and libraries. They include:
- Angular
- React
- JQuery
- Vue.js
- Meteor
- Node.js
- Ember.js
JavaScript History
In 1993, Mosaic, the first popular web browser, came into existence. In the year 1994, Netscape was founded by Marc Andreessen. He realized that the web needed to become more dynamic. Thus, a 'glue language' was believed to be provided to HTML to make web designing easy for designers and part-time programmers. Consequently, in 1995, the company recruited Brendan Eich intending to implement and embed Scheme programming language to the browser. But, before Brendan could start, the company merged with Sun Microsystems for adding Java into its Navigator so that it could compete with Microsoft over the web technologies and platforms. Now, two languages were there: Java and the scripting language. Further, Netscape decided to give a similar name to the scripting language as Java's. It led to 'Javascript'. Finally, in May 1995, Marc Andreessen coined the first code of Javascript named 'Mocha'. Later, the marketing team replaced the name with 'LiveScript'. But, due to trademark reasons and certain other reasons, in December 1995, the language was finally renamed to 'JavaScript'. From then, JavaScript came into existence.
Application of Javascript
- Web application
- Web Development
- Mobile Application
- Game Development
- Client-side Validation
- Server Applications
- Web Server
- Presentations and slideshows
- Dynamic drop-down menus
JavaScript is implemented using JavaScript statements that are placed within script in HTML head.
Case Sensitivity
JavaScript is a case-sensitive language. This means that the language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters. City and city in JavaScript have two very different meanings.
A variable is a storage location. There are a set of rules one needs to observe when naming variables:
- Name must start with a letter (a to z or A to Z).underscore( _ ), or dollar( $ ) sign.
- Names can contain letters, digits, underscores, and dollar signs.
- Name must not be a reserved words.
- After first letter we can use digits (0 to 9), for example value1.
- JavaScript variables are case sensitive, for example x and X are different variables
Variable Syntax
When declaring a variable one must declare with either let or const. If you want a general rule: always declare variables with const. If you think the value of the variable can change, use let. For example:
const name; // declares a variable called name.
or
let Area; //declares a variable called area
//initializes a variable called width to 5
let width=5;
Javascript Variable Scope
In javascript there are two types of variables:
- Local Variable - A local variable is declared inside a function. It can only be accessible in that function.
- Global Variable - A global variable has global scope which means it can be defined anywhere in your JavaScript code.
//local variables
function sum(){
let x = 10;
let y = 5;
let z = x + y;
}
//global variable
let greetings = Buenos días;
functionmorning(){
console.log(greetings);
}//the function above will output Buenos días
var multiply = 10 * 20;
- Arithmetic operators
- Comparison Operators
- Bitwise operators
- Logical operators
- Assignment Operators
- Special Operators
Arithmetic Operators
Arithmetic operators perform arithmetic operation on operands. They include:
Operator | Description | Example |
---|---|---|
+ | Addition | 60+20 = 80 |
- | Subtraction | 20-15 = 5 |
* | Multiplication | 5*20 = 100 |
/ | Division | 20/10 = 2 |
% | Modulus (Remainder) | 20%10 = 0 |
++ | Increment | var b=20; a++; Now b = 21 |
-- | Decrement | var b=20; a--; Now b = 19 |
Comparison Operators
Comparison operators compare values between two operands. They include:
Operator | Description | Example |
---|---|---|
== | Is equal to | 10==20 = false |
=== | Identical (equal and of same type) | 10==20 = false |
!= | Not equal to | 10!=20 = true |
!== | Not Identical | 20!==20 = false |
> | Greater than | 20>10 = true |
>= | Greater than or equal to | 20>=10 = true |
< | Less than | 20<10 = false |
<= | Less than or equal to | 20<=10 = false |
Bitwise Operators
Operator | Description | Example |
---|---|---|
& | Bitwise AND | (10==20 & 20==33) = false |
| | Bitwise OR | (10==20 | 20==33) = false |
^ | Bitwise XOR | (10==20 ^ 20==33) = false |
~ | Bitwise NOT | (~10) = -10 |
<< | Bitwise Left Shift | (10<<2) = 40 |
>> | Bitwise Right Shift | (10>>2) = 2 |
>>> | Bitwise Right Shift with Zero | (10>>>2) = 2 |
Logical Operators
Operator | Description | Example |
---|---|---|
&& | Logical AND | (10==20 && 20==33) = false |
|| | Logical OR | (10==20 || 20==33) = false |
! | Logical Not | !(10==20) = true |
Assignment Operators
The following operators are known as JavaScript assignment operators
Operator | Description | Example |
---|---|---|
= | Assign | 10+10 = 20 |
+= | Add and assign | var a=10; a+=20; Now a = 30 |
-= | Subtract and assign | var a=20; a-=10; Now a = 10 |
*= | Multiply and assign | var a=10; a*=20; Now a = 200 |
/= | Divide and assign | var a=10; a/=2; Now a = 5 |
%= | Modulus and assign | var a=10; a%=2; Now a = 0 |
Special Operators
The following operators are known as JavaScript special operators
Operator | Description |
---|---|
(?:) | Conditional Operator returns value based on the condition. It is like if-else. |
, | Comma Operator allows multiple expressions to be evaluated as single statement. |
delete | Delete Operator deletes a property from the object. |
in | In Operator checks if object has the given property |
instanceof | checks if the object is an instance of given type |
new | creates an instance (object) |
typeof | checks the type of object. |
void | it discards the expression's return value. |
yield | checks what is returned in a generator by the generator's iterator. |
A function is a reusable block of code that performs a specific task.
Function Syntax
A JavaScript function is defined with the function keyword, followed by a function name, followed by parentheses ().
function functionname(){
//block of code
}
function identifier(parameter 1, parameter 2, parameter 3){
//block of code
}
Calling a function
To call a function, you type the function name followed by parentheses.
//create a function called animal
function animal(){
console.log("I love dogs");
}
//calls the function animal
animal();
Return statement
To pass information from a function, Javascript uses return statement. When JavaScript reaches a return statement, the function will stop executing.
function rectanglearea(){
let width=10;
let height=5;
let area=width*height;
return area;
}
JavaScript comments are meaningful way to explain complex code in simple plain English. Comments can also be used to add suggestions or put warnings in code. Any JavaScript comment is ignored by the web browser. There are two different ways to write comments in Javascript on a;
Single line comments
Single line comments begin with two forward slashes (//).
//Declare variable X and give it a value of 10. let X=10;
//Declares variable Y and adds 5 to the variable X
let Y=5+X
Multiline (Block) comments
/*
This is a multiline comment 1
This is a multiline comment 2
This is a multiline comment 3
This is a multiline comment 4
This is a multiline comment 5
*/