JavaScript is a high-level, lightweight, and versatile programming language used to create interactive and dynamic web applications. It runs directly in the browser and can also be used on the server with Node.js. JavaScript is widely used for frontend development, backend development, web APIs, and modern full-stack applications.
JavaScript is a high-level, lightweight programming language used to make web pages interactive. It runs directly in the browser and is also used on the server with Node.js. Today, JavaScript is used to build websites, mobile apps, desktop applications, and APIs.
console.log("Hello JavaScript");
Output
Hello JavaScript
var has function scope and can be redeclared. let has block scope and its value can be changed but cannot be redeclared in the same scope. const also has block scope, but once assigned, it cannot be reassigned.
let age = 24;
age = 25;
const country = "India";
console.log(age);
console.log(country);
Output
25
India
JavaScript is a dynamically typed language, which means you don't need to declare the data type of a variable. The JavaScript engine automatically determines the type based on the assigned value.
let name = "Kartik";
let age = 24;
let isStudent = true;
console.log(typeof name);
console.log(typeof age);
console.log(typeof isStudent);
Output
string
number
boolean
JavaScript has seven primitive data types:
String
Number
Boolean
Undefined
Null
Symbol
BigInt
Primitive values are immutable and store a single value.
A function is a reusable block of code that performs a specific task. Functions reduce duplicate code, improve readability, and make applications easier to maintain and test.
Arrow functions provide a shorter syntax for writing functions. They are commonly used for callbacks, array methods, and situations where you want to preserve the surrounding this value.
const square = num => num * num;
console.log(square(5));
Output
25
The == operator compares values after performing type conversion if necessary. The === operator compares both value and data type without type conversion. In most cases, === is recommended because it avoids unexpected results.
Conditional statements allow JavaScript to execute different blocks of code based on whether a condition is true or false. Common statements include if, else if, else, and switch.
let age = 20;
if (age >= 18) {
console.log("Eligible");
} else {
console.log("Not Eligible");
}
Output
Eligible
Loops execute the same block of code repeatedly until a condition becomes false. JavaScript provides for, while, do...while, for...of, and for...in loops.
for (let i = 1; i <= 5; i++) {
console.log(i);
}
Output
1
2
3
4
5
Arrays are used to store multiple values in a single variable. They are ordered, can contain different data types, and provide many built-in methods for adding, removing, searching, and transforming data.
const fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[1]);
Output
Banana
Objects store data as key-value pairs. They are used to represent real-world entities like users, products, or orders. Along with properties, objects can also contain methods (functions).
const user = {
name: "Sam",
age: 24
};
console.log(user.name);
Output
Sam
undefined means a variable has been declared but hasn't been assigned a value. null is an intentional value assigned by the developer to represent "no value" or an empty object reference.
let a;
let b = null;
console.log(a);
console.log(b);
Output
undefined
null
Hoisting is JavaScript's default behavior of moving variable and function declarations to the top of their scope before execution. Only declarations are hoisted, not initializations.
console.log(name);
var name = "Kartik";
Output
undefined
Scope determines where a variable can be accessed in a program. JavaScript provides Global Scope, Function Scope, and Block Scope. Variables declared with let and const are block-scoped.
A closure is created when an inner function remembers and accesses variables from its outer function, even after the outer function has finished executing. Closures are useful for data privacy and maintaining state.
function outer() {
let count = 0;
return function () {
count++;
console.log(count);
};
}
Output
1
2
const counter = outer();
counter();
counter();
A callback is a function passed as an argument to another function. It is executed after a task is completed and is commonly used in asynchronous operations such as timers, file reading, and API requests.
function greet(name, callback) {
console.log("Hello " + name);
callback();
}
greet("Sam", function () {
console.log("Welcome");
});
Output
Hello Sam
Welcome
Let's discuss how we can help you achieve your goals. Book a free 30-minute strategy call with our experts.