Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Day -5 JavaScript Fundamentals

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Day -5 JavaScript Fundamentals

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

JavaScript Fundamentals

1. Introduction to JavaScript

JavaScript is a versatile, dynamic programming language used to make web pages interactive. Unlike
HTML and CSS, which are used for structure and styling respectively, JavaScript is a scripting language
that allows you to control the behavior of web pages. It is an essential technology used in web
development and runs on all modern browsers. JavaScript is primarily used for front-end
development to create dynamic, interactive user experiences but can also be used for back-end
development through technologies such as Node.js.

2. Variables

In JavaScript, variables are containers that hold data values. You declare a variable using one of three
keywords: var, let, and const. Each has its own behavior in terms of scoping and reassignment.

 var: The oldest form of declaring variables, var is function-scoped. It can be redeclared and
reassigned within its scope.

 let: Introduced in ES6 (ECMAScript 2015), let is block-scoped, meaning it only exists within
the block {} where it's defined. It can be reassigned but not redeclared.

 const: Also block-scoped, but once assigned, a const variable cannot be reassigned, making it
ideal for immutable values.

Example:

let name = "Alice"; // let allows reassignment

const pi = 3.14; // const does not allow reassignment

var age = 30; // var has function scope

3. Data Types

JavaScript supports various data types, including primitive types and non-primitive (reference) types.

 Primitive Types: These are simple types that represent a single value.

o string: Represents text (e.g., "Hello World").

o number: Represents both integer and floating-point numbers (e.g., 5, 3.14).

o boolean: Represents true or false.

o null: Represents an intentional absence of value.

o undefined: Represents a variable that has been declared but not yet assigned a
value.

o symbol: A unique identifier for object properties (used less frequently).

 Non-Primitive Types: These types are used to store collections of data.

o Objects: Used to store collections of key-value pairs.

o Arrays: A type of object used to store ordered collections of data.


Example:

let name = "John"; // string

let age = 25; // number

let isStudent = true; // boolean

let user = { name: "Alice", age: 30 }; // object

let colors = ["red", "green", "blue"]; // array

4. Operators

Operators in JavaScript are symbols that perform operations on variables and values. The most
common types of operators are arithmetic, comparison, and logical operators.

 Arithmetic Operators: Used for mathematical operations.

o +, -, *, /, % (modulus), ++ (increment), -- (decrement).

Example:

let a = 5;

let b = 3;

console.log(a + b); // 8 (addition)

console.log(a - b); // 2 (subtraction)

console.log(a * b); // 15 (multiplication)

 Comparison Operators: Used to compare two values.

o ==, ===, !=, !==, <, >, <=, >=.

Example:

console.log(5 == '5'); // true (== compares values only)

console.log(5 === '5'); // false (=== compares values and types)

 Logical Operators: Used to perform logical operations.

o && (AND), || (OR), ! (NOT).

Example:

console.log(true && false); // false

console.log(true || false); // true

console.log(!true); // false

5. Functions

Functions in JavaScript allow you to encapsulate reusable blocks of code. Functions can be declared
in various ways.

 Function Declaration:
function greet(name) {

return "Hello, " + name;

 Arrow Function (ES6):

const greet = (name) => "Hello, " + name;

Functions can also accept arguments and return values.

Example:

function add(a, b) {

return a + b;

console.log(add(2, 3)); // Output: 5

You might also like