[Go to site: main page, start]

0% found this document useful (0 votes)
4 views4 pages

Java Script

JavaScript is a high-level, interpreted programming language that enables interactive and dynamic web pages. It includes variable declarations using var, let, and const, and supports various data types categorized into primitive and non-primitive types. The document also covers condition statements, loops, arrays, and functions, detailing their syntax and usage.
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 views4 pages

Java Script

JavaScript is a high-level, interpreted programming language that enables interactive and dynamic web pages. It includes variable declarations using var, let, and const, and supports various data types categorized into primitive and non-primitive types. The document also covers condition statements, loops, arrays, and functions, detailing their syntax and usage.
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

JavaScript:

JavaScript is a high-level, interpreted programming language used to make web


pages interactive and dynamic.
Variables
A variable is like a container that holds data that can be reused or updated later in
the program. In JavaScript, variables are declared using the keywords var, let,
or const.
1. var Keyword
The var keyword is used to declare a variable. It has a function-scoped or globally-
scoped behaviour.
var n = 5;
[Link](n);
var n = 20; // reassigning is allowed
[Link](n);
2. let Keyword
The let keyword is introduced in ES6, has block scope and cannot be re-declared in
the same scope.
let n= 10;
n = 20; // Value can be updated
// let n = 15; //can not redeclare
[Link](n)
3. const Keyword
The const keyword declares variables that cannot be reassigned. It's block-scoped as
well.
const n = 100;
// n = 200; This will throw an error
[Link](n)
Data Types
JavaScript supports various datatypes, which can be broadly categorized into
primitive and non-primitive types.
Primitive Datatypes
Primitive datatypes represent single values and are immutable.
1. Number: Represents numeric values (integers and decimals).
let n = 42;
let pi = 3.14;
2. String: Represents text enclosed in single or double quotes.
let s = "Hello, World!";
3. Boolean: Represents a logical value (true or false).
let bool= true;
4. Undefined: A variable that has been declared but not assigned a value.
let notAssigned;
[Link](notAssigned);
5. Null: Represents an intentional absence of any value.
let empty = null;
6. Symbol: Represents unique and immutable values, often used as object keys.
let sym = Symbol('unique');
7. BigInt: Represents integers larger than Number.MAX_SAFE_INTEGER.
let bigNumber = 123456789012345678901234567890n;
Non-Primitive Datatypes:
Non-primitive types are objects and can store collections of data or more complex
entities.
1. Object: Represents key-value pairs.
let obj = {
name: "Amit",
age: 25
};
2. Array: Represents an ordered list of values.
let a = ["red", "green", "blue"];
3. Function: Represents reusable blocks of code.
function fun() {
[Link]("GeeksforGeeks");
}

Condition Statements:
A condition is a statement that evaluates to true or false and is used to control the
flow of a program.
if Statement:
Executes a block of code only if the condition is true

if (condition) {
// code runs if true
}
if-else Statement:
Executes one block if condition is true, otherwise executes another block
if (condition) {
// true block
} else {
// false block
}
else if Ladder:
Used to check multiple conditions sequentially
if (condition1) {
} else if (condition2) {
} else {
}
Nested if:
An if statement inside another if statement
if (condition1) {
if (condition2) {
// code
}
}

Ternary Operator:
A short form of if-else used for simple conditions
condition ? value1 : value2;

Truthy / Falsy:
Values that are automatically converted to true or false in conditions
 Falsy: false, 0, "", null, undefined, NaN
 Truthy: everything else

LOOPS :
A loop is used to execute a block of code repeatedly until a condition becomes false.

for Loop:
Used when the number of iterations is known.
for (initialization; condition; update) {
// code
}
while Loop:
Repeats code while the condition is true.
while (condition) {
// code
}
do...while Loop:
Executes code at least once, then checks the condition.
do {
// code
} while (condition);
Arrays:
An array is a collection of multiple values stored in a single variable.
let arr = [10, 20, 30];

Functions:
A function is a block of code designed to perform a specific task.
It runs only when it is called (invoked).
Function Declaration
function greet() {
[Link]("Hello");
}
greet();
✔ Can be called before declaration

2. Function Expression
const greet = function() {
[Link]("Hello");
};
greet();
✔ Stored in a variable
❌ Cannot call before definition

3. Arrow Function (ES6)


const add = (a, b) => a + b;

[Link](add(2, 3));
✔ Short syntax
✔ No function keyword

4. Anonymous Function
setTimeout(function() {
[Link]("Hi");
}, 1000);
✔ No name
✔ Used as arguments

5. Callback Function
function process(callback) {
callback();
}

process(function() {
[Link]("Callback executed");
});

You might also like