It starts from the basics of JavaScript.
By Kuldeep
2024-09-06
JavaScript is a versatile and widely-used programming language primarily known for its role in web development. It enables interactive features on web pages and can be used on both the client and server sides.
Variables are containers for storing data values. JavaScript has several data types, including:
let name = "John";
const age = 30;JavaScript has three ways to declare variables: var, let, and const. It supports different data types such as numbers, strings, booleans, arrays, objects, etc.
// Variable declaration
let name = "John"; // String
const age = 30; // Number
var isStudent = true; // Boolean
// Arrays and Objects
let hobbies = ["Reading", "Sports"]; // Array
let person = { // Object
firstName: "John",
lastName: "Doe",
age: 30,
};
console.log(name, age, isStudent, hobbies, person);Functions are blocks of code designed to perform a particular task. They can be declared in different ways:Function Declaration: Defines a function with a name.Function Expression: Defines a function and assigns it to a variable.Arrow Function: A concise way to write functions.
function greet(name) {
return `Hello, ${name}!`;
}
// Function Expression
const add = function(a, b) {
return a + b;
};
// Arrow Function
const multiply = (a, b) => a * b;
// Calling functions
console.log(greet("Alice")); // Output: Hello, Alice!
console.log(add(5, 10)); // Output: 15
console.log(multiply(2, 3)); // Output: 6