Introduction to JavaScript
JavaScript is a versatile, high-level programming language used for
web development. It enables dynamic, interactive features on web
pages, such as forms, animations, and other engaging content, running
directly in the browser.
History: Developed by Brendan Eich in 1995, JavaScript has
evolved as a crucial part of the web technology stack (HTML,
CSS, JavaScript).
Purpose: JavaScript can be used for front-end (client-side) as
well as back-end (server-side, with Node.js) development.
Features:
o Dynamic and Interpreted: JavaScript is interpreted,
meaning code is run line-by-line by the browser.
o Weakly Typed: Variables do not require a defined type,
making it flexible but sometimes prone to unexpected
errors.
o Prototype-Based Object Orientation: JavaScript uses
prototypes rather than class inheritance (ES6 introduced
classes for a more familiar syntax).
JavaScript Syntax
Syntax is the set of rules defining the structure of JavaScript code.
1. Case Sensitivity:
o JavaScript is case-sensitive, meaning variable and Variable
are different.
2. Statements and Semicolons:
o Each command or instruction in JavaScript is a statement.
Statements are typically separated by semicolons (;),
although JavaScript can often interpret them without it.
o Example:
let name = "Alice";
console.log(name);
3. Code Blocks and Indentation:
o Code blocks are defined by curly braces {}.
o Indentation is not mandatory but is highly recommended
for readability.
o Example:
if (true) {
console.log("This is true.");
}
4. Comments:
o JavaScript comments allow you to add explanations or
temporarily disable parts of code.
JavaScript Comments
Comments are ignored by the JavaScript interpreter, making them
useful for documenting code.
1. Single-Line Comment:
o Denoted by //, it comments out the rest of the line.
o Example:
// This is a single-line comment
let x = 5; // x is assigned the value 5
2. Multi-Line Comment:
o Begins with /* and ends with */, allowing multiple lines of
comments.
o Example:
/* This is a
multi-line comment */
let y = 10;
JavaScript Variables
Variables are containers for storing data values. JavaScript provides
three keywords to declare variables: var, let, and const.
Declaring Variables
1. var:
o The original way to declare variables in JavaScript, now
considered outdated due to its issues with scope.
o Example:
var name = "John";
2. let:
o Introduced in ES6, let is block-scoped, meaning it is only
accessible within the block it is defined.
o Example:
let age = 25;
3. const:
o Also introduced in ES6, const is used for variables that are
not meant to be reassigned (constants).
o Example:
const PI = 3.14;
Variable Naming Rules
Must start with a letter, underscore _, or dollar sign $.
Cannot start with a number, and no special characters or spaces
are allowed.
Variable names are case-sensitive and should use camelCase for
readability.
Data Types in Variables
JavaScript is a dynamically typed language, meaning you do not need
to specify the data type when declaring a variable. The data type is
determined based on the value assigned.
1. String:
o Text enclosed in single ('), double ("), or template literals
(`).
o Example:
let greeting = "Hello, world!";
let name = `John`;
2. Number:
o Can be an integer or floating-point number.
o Example:
let age = 30;
let price = 19.99;
3. Boolean:
o Can be either true or false.
o Example:
let isStudent = true;
4. Undefined:
o A variable that has been declared but not assigned a value.
o Example:
let x;
console.log(x); // Outputs: undefined
5. Null:
o Represents an explicitly empty or non-existent value.
o Example:
let emptyValue = null;
6. Object:
o Used for storing collections of data and more complex
entities.
o Example:
let person = {
name: "Alice",
age: 28
};
Example of Variables and Data Types:
let firstName = "Emma"; // String
let age = 30; // Number
let isEmployed = true; // Boolean
let lastName; // Undefined
let netWorth = null; // Null
console.log(firstName, age, isEmployed, lastName, netWorth);