JavaScript 2
JavaScript 2
JavaScript is a versatile, high-level, and interpreted programming language primarily used for
web development. It allows developers to create interactive and dynamic web pages. JavaScript
is executed on the client side within web browsers but can also be used on the server side with
environments like Node.js.
---
1. Variables in JavaScript
Variables in JavaScript are used to store data values. JavaScript provides three ways to declare
variables:
Example:
var x = 10;
console.log(x); // Output: 10
let is block-scoped, meaning it is accessible only within the block {} where it is declared.
Example:
let y = 20;
y = 30; // Allowed
console.log(y); // Output: 30
The value assigned to a const variable must be initialized at the time of declaration.
Example:
const z = 50;
console.log(z); // Output: 50
---
2. Functions in JavaScript
Functions in JavaScript are reusable blocks of code that perform a specific task. There are
different types of functions:
(i) Function Declaration
Example:
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Alice")); // Output: Hello, Alice
Example:
Example:
Example:
(function () {
console.log("This runs immediately!");
})();
---
4. Operators in JavaScript
Conclusion
JavaScript is a powerful language with flexible variable declarations, reusable functions, built-in
default functions, and essential operators. Understanding these fundamentals is crucial for
mastering JavaScript development.