Javascript has grown to become one of the most popular programming languages used for creating dynamic and interactive web applications. This article provides a detailed guide to learning Javascript from the basics to advanced concepts using Javatpoint’s Javascript tutorial.Javascript is an object-oriented scripting language that is used to make webpages interactive. It was created by Brendan Eich in 1995. Javascript can enhance HTML pages by validating forms, creating cookies, detecting browsers, creating dynamic styling, and more. It allows real-time communication between a client and a server.
What is Javascript?
Javascript is a lightweight, interpreted programming language that is used to make web pages dynamic and interactive.
- Javascript runs on the client-side of the web, which enables direct interaction with the HTML, unlike server-side languages like PHP. (Context)
- It enhances HTML pages by providing interactivity and simplicity. Popular websites like Google Maps, Facebook, and Youtube use Javascript. (Competitor 1)
Javascript Features
- Object-based scripting language
- Follows syntax of C programming
- Weakly typed language
- Supports prototypes for inheritance
- Interpreted language
- Case sensitive
- Cross-platform support
Javascript Syntax
The syntax of Javascript is similar to other C-family languages. It includes features like:
- Keywords like var, function, if, else
- Expressions and operators like arithmetic, comparison, logical
- Control flow statements like if-else, switch, for loop
- Comments – single and multi-line
- Variables to store data
Core Concepts
- Variables and Data Types
- Operators
- Decision Making and Loops
- Functions
- Objects and Arrays
Variables and Data Types
- var keyword to declare variables
- Primitive types: number, string, boolean
- Complex types: object, array
- Global and local scope
// Example
var name = "John"; // String
var age = 30; // Number
var isStudent = true; // Boolean
Variables declared with var are function scoped while those declared with let and const are block scoped.
Operators
- Arithmetic operators for mathematical operations like +, -, *, /
- Comparison operators for conditional logic like ==, ===, >, <
- Logical operators like &&, ||, !
- Assignment operators like =, +=
// Arithmetic
let x = 5 + 6;
// Comparison
if (x > 10) {
// do something
}
// Logical
if (x > 5 && x < 10) {
// do something
}
// Assignment
let y = 10;
y += 5; // y = y + 5
Decision Making and Loops
- Conditional statements like if-else
- Switch case for multiway branching
let num = 5;
if (num > 0) {
console.log("Positive number");
} else {
console.log("Negative number");
}
switch(num) {
case 1:
// do something
break;
case 2:
// do something
break;
default:
// do something
}
- Loops like for, while, do-while
- Break and continue statements
// For loop
for (let i = 0; i < 5; i++) {
// Executes 5 times
}
// While loop
let j = 0;
while (j < 5) {
j++;
}
// Do While
let k = 0;
do {
k++;
} while (k < 5);
Functions
- User-defined functions using function keyword
- Parameters and return values
- Anonymous functions
- Immediately Invoked Function Expressions (IIFE)
// Regular function
function add(num1, num2) {
return num1 + num2;
}
// Anonymous
let sub = function(num1, num2) {
return num1 - num2;
}
// IIFE
(function() {
// logic here
console.log("Hello");
})();
Objects and Arrays
- Objects store data as key-value pairs
- Arrays store ordered collection of values
- Creating, accessing properties and methods
// Object
let person = {
name: "John",
age: 30,
greet: function() {
console.log("Hello");
}
}
// Access property
person.name
// Call method
person.greet();
// Array
let fruits = ["Apple", "Orange", "Banana"];
fruits[0]; // Access element
fruits.push("Mango"); // Add element
DOM Manipulation
DOM stands for Document Object Model. It allows manipulating HTML/CSS using Javascript.
- Methods like getElementById, querySelector to select elements
// Get element by ID
let elem = document.getElementById("myId");
// Query selector
let elems = document.querySelectorAll("p");
- Changing styles, attributes, classes
elem.style.color = "red";
elem.setAttribute("class", "myClass");
elem.classList.add("myClass");
- Adding/removing elements
let newElem = document.createElement("p");
elem.appendChild(newElem);
elem.removeChild(childElem);
This allows creating highly dynamic web pages that can interactively respond to user input.
Events
- Common events like click, submit, keydown, mouseover etc.
- Event listeners to trigger callback functions
- Event propagation concepts like bubbling and capturing
// Click event
btn.addEventListener("click", function() {
// do something
});
// With event argument
btn.addEventListener("click", function(event) {
event.preventDefault(); // Prevent default behavior
})
Event handling allows detecting user interactions and responding to them.
Async Programming
- Callbacks, Promises, Async/Await to handle asynchronous logic
- Avoid callback hell
function delayedLog(item) {
// Simulate delay
setTimeout(function() {
console.log(item);
}, 1000);
}
// Callback hell
delayedLog("A");
delayedLog("B");
delayedLog("C");
// Promise
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
delay(1000).then(() => {
// runs after 1 second
});
// Async/Await
async function asyncCall() {
console.log("A");
await delay(1000);
console.log("B");
}
This allows efficient asynchronous programming like calling APIs, file I/O etc.
Conclusion
Javascript is an essential language for web development. This guide summarizes the basics like syntax, data types, operators to advanced concepts like DOM manipulation and asynchronous programming. Javatpoint’s Javascript tutorial provides a comprehensive resource to master this language. With its wide range of applications and features, Javascript is a must-learn skill for every web developer.