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

JavaScript Notes

Uploaded by

Ît'x Malik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

JavaScript Notes

Uploaded by

Ît'x Malik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

1.

‬‭Using
‭ a Web Browser Console :‬
‭You can open the developer console in your web browser and type‬
‭JavaScript directly into it.‬
2.‬‭Html
‭ File with‬‭<script>‬‭Tag :‬
‭You can create an HTML file and include JavaScript within‬‭<script>‬‭tags.‬
‭ hen you open the HTML file in a web browser, the JavaScript will be executed.‬
W

3.‬‭External
‭ JavaScript File :‬
‭ ou can write your JavaScript code in a separate‬‭.js‬‭file and include it in‬
Y
‭your HTML file using the‬‭<script>‬‭tag with the‬‭src‬ ‭attribute.‬
4.‬‭Node.js‬

I‭ f you want to execute JavaScript outside of the browser, you can use‬
‭Node.js, which is a JavaScript runtime built on Chrome's V8 JavaScript engine. You‬
‭can create a‬‭.js‬ ‭file and run it using Node.js from the command line.‬

‭JavaScript :‬

‭To Execute :‬
Different data type can be stored in variable
Such as string,number,array.
Why is typeof Null Object?
That JavaScript was used for null and in specification typeof null was written as object and
many javascript experts consider it as a mistake but just say that because the spec is very old
and it is written in the spec that typeof null is an object and a lot of code is relied on it, so we
cannot change it. We cannot null typeof null we will object only.
What is Expression in JavaScript?

Expression are any value inside javascript that you can assign to a variable, a constant,it can also become
a js program by itself.

FOR EXAMPLE : if i wrote (!true), it is a js program by it self. if i just wrote (7;), it's a js program by it self
and you execute it then it won't give an error.

What is Operators in JavaScript?

We do computation in most of the cases inside any program and to do this kind of computation we have
to use operators.

EXAMPLE 1 : when we write (7+5=12): so 7 and 5 are our operands, and (+) is our operator an 12 is
our result Not necessary: our operands are 2 and the operator is only 1.

EXAMPLE 2 : if we write (!true) (!) is our operator and (true) is our operand and false is our result (!true
= false).

Arithmetic operators in JavaScript :

Arithmetic operators are used to perform mathematical operations on numbers. The most commonly
used arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). In addition
to these basic, JavaScript also provides the modulus operator (%), exponentiation operator (**), and
increment/decrement operators (++ and --).

Assignment operators in JavaScript :

Assignment operators are used to assign values to variables. They are used to perform a specific
operation and then assign the result to a variable. There are several different types of assignment
operators in JavaScript, including the "=" operator, which is used to assign a value a variable; "+="
operator, which is used to add a value to a variable; "-=" operator, which is used to subtract a value from
a variable; "*=" operator, which is used to multiply a value to a variable; "/=" operator , which is used to
divide a value from a variable; "%=" operator, which is used to assign the remainder after division to a
variable.

Comparison operators in JavaScript :

comparison operators are fundamental JavaScript operators used to compare two values. They return a
Boolean value (true or false) indicating whether the comparison is true or false.
let a = 5;
let b = 5; true
 == Equal to: returns true if the operands are equal x == y

 != Not equal to: returns true if the operands are not equal x != y

 === Strict equal to: true if the operands are equal and of the same type x === y
 !== Strict not equal to: true if the operands are equal but of different type or not equal at all
x !== y

 > Greater than: true if left operand is greater than the right operand x>y

 >= Greater than or equal to: true if left operand is greater than or equal to the right
operand x >= y

 < Less than: true if the left operand is less than the right operand x < y

 <= Less than or equal to: true if the left operand is less than or equal to the right operand
x <= y

Logical operators in JavaScript :

In JavaScript are used to combine and manipulate Boolean values (true/false). The three main logical
operators are && (AND), || (OR), and ! (NOT).

1. The && operator returns true only if both operands are true. For example, "5 > 3 && 7 <10"
would return true because both conditions are true.

2. The || operator returns true if either of the operands is true. For example, "6 < 4 || 8 > 5" would
return true because the second condition is true.

3. The ! operator returns the opposite of the Boolean value of the operand. For example, "!false"
would return true and "!true" would return false.

let a = 5;
let b = 6;
console.log("a == b", a == b)
console.log("a === b", a === b)
console.log("a != b", a != b)
console.log("a !== b", a !== b)
console.log("a > b ", a > b)
console.log("a >= b ", a >= b)
console.log("a < b", a < b)
console.log("a <= b", a <= b)
What is Conditional Statement in JavaScript?

The Conditional Statement in JavaScript is a programming construct that allows you to execute a set of
statement based on a condition.It execute a statement if the given condition is true and ignores when
the conditon is false.It is also called decision-making structure OR selection structure.

What is IF Statement in JavaScript?

The 'if statement' in JavaScript is a conditional statement that executes a block of code if a specified
condition evaluates to true. If the condition is false, the code block is skipped.

Syntax :

if (condition) {

// Code to execute if the condition is true

Example :

let a = prompt("Hey What's Your age")

a = Number.parseInt(a)

if (a > 0 ) {
console.log("This is a valid age")

else{

alert("This is an invalid age")

What is IF-else Statement in JavaScript?

An "if-else statement" in JavaScript is a conditional construct that allows you to execute one block of
code if a specified condition is true and another block of code if the condition is false, providing an
alternative path of execution.

Syntax :

if (condition) {

// Code to execute if the condition is true

} else {

// Code to execute if the condition is false

Example :

let isRaining = true;

if (isRaining) {

console.log("Don't forget to bring an umbrella!"); // This will be executed because isRaining is true.

} else {

console.log("No need for an umbrella today.");

What is IF-else if Statement in JavaScript?

The 'if-else if statement' is a conditional construct in JavaScript that allows you to evaluate multiple
conditions sequentially. It executes the code block associated with the first true condition, providing an
alternative set of actions when the initial condition is false.

Syntax :

if (condition1) {

// Code to execute if condition1 is true

} else if (condition2) {
// Code to execute if condition2 is true

} else {

// Code to execute if neither condition1 nor condition2 is true (optional)

Example :

let temperature = prompt("What's the weather today?")

if (temperature >= 30) {

console.log("It's hot outside!"); // This will not be executed because the first condition is false.

} else if (temperature >= 20) {

console.log("The weather is pleasant."); // This will be executed because the second condition is true.

} else {

console.log("It's cold outside.");

Ternary Operator in JavaScript :

The ternary operator in JavaScript is a shorthand conditional statement that allows you to assign a value
to a variable based on a specified condition.

Syntax :

The syntax `condition ? value If True : value If False`.

Example :

let age = prompt("What's your age?")

let message = age >= 18 ? "You are an adult" : "You are a minor";

console.log(message);
i = 0; i > 12; i++
What is Loops in JavaScript?

A type of control structure that repeats a statement or set of statement is known as looping structure. it
is also known as iterative or repeative structure.

What is For Loop in JavaScript?

for loop executes one or more statements for a specified number of times. This loop is also called
counter-controlled loop. It is the most flexible loop. That is why, it is the most frequently-used loop by
the programmers.

Example 1 :

OutPut :

Example 2:

Output :
What is For "in" Loop in JavaScript?

The for...in loop in JavaScript is a way to go through an object's properties, allowing you to work with
each property or key-value pair.

Example :

Output :

What is For "of " Loop in JavaScript?

"For...of" loop in JavaScript is used to iterate over the elements of an iterable object, making it
convenient to access and work with each element.

Example :
OutPut :

What is While Loop in JavaScript?

The while loop is the simplest loop in the JavaScript language. This loop executes one or more
statements while the given condition remains true. It is particularly useful when the number of iterations
is not known in advance.In a while loop, first, the condition will be checked, and then the block will run.

Syntax :

while (condition) {

// Statements to be executed as long as the condition is true

Example :

OutPut :
What is Do-While Loop in JavaScript?

The do-while loop is an iterative control structure in JavaScript and many other programming languages.
This loop executes one or more statements while the given condition is true. The unique feature of the
do-while loop is that the condition is checked after the body of the loop is executed, which ensures that
the loop is executed at least once.In a do-while loop the Block runs then condition checks So, do while
loop is executed.

Syntax :

do {

// Code to be executed

} while (condition);

Example :

Output :

What is Function in JavaScript?


A function is a block of code that performs a specific task in JavaScript. A program in JavaScript can have
multiple functions, and each function must have a unique name. In every JavaScript program, there is at
least one function known as 'main' or 'entry point,' which is the starting point of the program's
execution.

Example :
What is String?
In JavaScript, a string is a sequence of characters used to represent text. Strings are enclosed in
single quotes (' '), double quotes (" "), or backticks ( ). They are immutable, meaning once
created, their content cannot be changed directly. Strings can contain letters, numbers,
symbols, and whitespace characters.
Example:

String Method?
1.`Lenght` : The `length` property returns the length of the string.

Example:

2.`charAt(index)`: The `charAt(index)` method returns the character at the specified index.
Example:

3.`toUpperCase()`: The `toUpperCase()` method converts the string to uppercase.

Example:

4.`toLowerCase()`: The `toLowerCase()` method converts the string to lowercase.

1
Example:

5.`slice`: The `slice(start, end)` method extracts a section of a string and returns it as a new
`string`, without modifying the original string. The start index is inclusive, and the `end` index is
exclusive.

Example:

6. `replace`: The `replace` method returns a new string. This method does not change the
original string.

Example:

7.`concat()`: The `concat` method is used to merge two or more strings. It does
not change the existing strings but returns a new string.

Example:

8.`trim()`: The trim() method removes whitespace from both ends of a string. Whitespace in
this context includes spaces, tabs, and newline characters.

2
Example:

What is Escape Sequence?


In JavaScript a escape sequence is a series of characters starting with a backslash (\) followed by
one or more characters that represent a special character.

Example:

⦁ `\n` represents a newline

⦁ `\t` represents a tab

⦁ `\"` represents a double quote

⦁ `\\` represents a backslash

3
What is Template litteral in JavaScript?
A template literal in JavaScript is a feature that allows you to embed expressions and variables
inside a string, denoted by backticks (`).

Example :

Output:

You might also like