JavaScript is a high-level, dynamic programming language commonly used for building interactive and dynamic websites. It was originally developed by Netscape as a way to add interactivity to web pages, and it's now one of the core technologies of the web, alongside HTML and CSS.
JavaScript is primarily used for:
-
Client-Side Scripting: Most often, JavaScript is executed in the web browser (client-side) to manipulate the content and structure of web pages. For example, it can be used to create interactive forms, handle events like button clicks, animate elements, or validate user input.
-
Server-Side Scripting: With the advent of technologies like Node.js, JavaScript can also be run on the server, enabling developers to write both client-side and server-side code using the same language.
-
Event-Driven Programming: JavaScript is event-driven, meaning it often runs in response to user actions like clicks, key presses, or page loading events.
-
Versatile and Extensible: JavaScript can interact with APIs (Application Programming Interfaces) to fetch or send data to servers, perform complex calculations, or even work with external libraries and frameworks, such as React, Angular, or Vue.js.
In essence, JavaScript makes web pages interactive, dynamic, and functional, offering developers the ability to build more engaging and feature-rich websites.
LESSION NO 2
Basic JavaScript Syntax and Variables
JavaScript has a straightforward syntax, and learning how to use variables and basic operators is key to writing effective code. Let’s break down the essentials.
1. Comments in JavaScript
Comments are useful for explaining what your code does or making notes for yourself and others.
-
Single-line comment:
// This is a single-line comment -
Multi-line comment:
/* This is a multi-line comment */
2. Variables in JavaScript
Variables are used to store data values. There are three ways to declare a variable in JavaScript:
-
var(older way, not recommended for modern JavaScript):var name = "John"; -
let(modern, used for variables that can change their value):let age = 30; age = 31; // The value of age can change -
const(used for constants or values that do not change):const birthYear = 1994; // birthYear = 1995; // This will cause an error because we can't reassign a const
Variable Naming Rules:
- Variables must begin with a letter, an underscore (
_), or a dollar sign ($). - Subsequent characters can be letters, numbers, underscores, or dollar signs.
- JavaScript is case-sensitive:
ageandAgeare different variables. - Reserved keywords (like
let,var,const,function, etc.) cannot be used as variable names.
3. Data Types in JavaScript
JavaScript has several data types that you can store in variables:
-
String: Text wrapped in single or double quotes.
let name = "Alice"; // A string let greeting = 'Hello'; // Another string -
Number: Integer or floating-point numbers.
let age = 25; // Integer let price = 9.99; // Float -
Boolean: Represents true or false.
let isStudent = true; let isAdult = false; -
Undefined: A variable that has been declared but not assigned a value.
let car; console.log(car); // Output: undefined -
Null: Represents an intentional absence of any value.
let address = null; -
Object: Used to store collections of data.
let person = { name: "John", age: 30 }; // An object with two properties -
Array: Used to store multiple values in a single variable.
let fruits = ["apple", "banana", "cherry"]; // An array
4. Basic Operators
-
Arithmetic Operators:
let a = 10; let b = 5; console.log(a + b); // Addition (Output: 15) console.log(a - b); // Subtraction (Output: 5) console.log(a * b); // Multiplication (Output: 50) console.log(a / b); // Division (Output: 2) console.log(a % b); // Modulus (remainder of division) (Output: 0) console.log(a ** b); // Exponentiation (a raised to the power of b) (Output: 100000) -
Comparison Operators (to compare values):
console.log(a == b); // Equal to (Output: false) console.log(a != b); // Not equal to (Output: true) console.log(a > b); // Greater than (Output: true) console.log(a < b); // Less than (Output: false) console.log(a >= b); // Greater than or equal to (Output: true) console.log(a <= b); // Less than or equal to (Output: false) -
Logical Operators:
let x = true; let y = false; console.log(x && y); // Logical AND (Output: false) console.log(x || y); // Logical OR (Output: true) console.log(!x); // Logical NOT (Output: false)
5. Template Literals (String Interpolation)
Instead of using + to concatenate strings, you can use template literals (enclosed in backticks `) to embed variables directly inside a string.
let firstName = "John";
let lastName = "Doe";
let fullName = `${firstName} ${lastName}`; // Using template literals
console

Comments
Post a Comment