0% found this document useful (0 votes)
11 views44 pages

JavaScript Operators, Loops, and Functions Guide

The document provides an overview of JavaScript concepts including comments, operators, conditional statements, loops, strings, arrays, functions, and array methods. It includes examples and practice questions for each topic to reinforce learning. The content is structured to guide users through fundamental programming concepts in JavaScript.

Uploaded by

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

JavaScript Operators, Loops, and Functions Guide

The document provides an overview of JavaScript concepts including comments, operators, conditional statements, loops, strings, arrays, functions, and array methods. It includes examples and practice questions for each topic to reinforce learning. The content is structured to guide users through fundamental programming concepts in JavaScript.

Uploaded by

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

Comments in JS

Part of Code which is not executed

lege
Col
pna
A
Operators in JS
Used to perform some operation on data

e
Arithmetic Operators

+, -, *, /
lleg
a Co
Modulus

Exponentiation Apn
Increment

Decrement
Operators in JS
Assignment Operators

= += -= *= %= **=

lege
Col
pna
A
Operators in JS
Comparison Operators

Equal to ==
ge
Equal to & type

le
===
Not equal to !=
Col
Not equal to & type !==

pna
>, >=, <, <= A
Operators in JS
Logical Operators

Logical AND &&

lege
Col
a
||
n
Logical OR

Logical NOT ! Ap
Conditional Statements
To implement some condition in the code

if Statement

lege
Col
pna
A
Conditional Statements
if-else Statement

lege
Col
pna
A
Conditional Statements
else-if Statement

lege
Col
pna
A
Operators in JS
Ternary Operators

ge
condition ? true output : false output

olle
na C
Ap
MDN Docs

lege
Col
pna
A
Let‘s Practice
Qs1. Get user to input a number using prompt(“Enter a number:”). Check if the number is
a multiple of 5 or not.

lege
Col
pna
A
Let‘s Practice
Qs2. Write a code which can give grades to students according to their scores:
80-100, A

e
70-89, B

eg
60-69, C
50-59, D

Coll
a
0-49, F

Apn
Loops in JS

lege
l
Loops are used to execute a piece of code again & again

a Co
pn
for Loop

A
for (let i = 1; i <= 5; i++) {

[Link]("apna college");

}
Loops in JS

lege
l
Infinite Loop : A Loop that never ends

a Co
Apn
Loops in JS

lege
ol
while Loop

na C
p
while (condition) {

}
A
// do some work
Loops in JS

lege
ol
do-while Loop

na C
do {

A
// do some workp
} while (condition);
Loops in JS

lege
ol
for-of Loop

na C
p
for (let val of strVar) {

}
A
//do some work
Loops in JS

lege
ol
for-in Loop

na C
p
for (let key in objVar) {

}
A
//do some work
Let‘s Practice
Qs1. Print all even numbers from 0 to 100.

lege
Col
pna
A
Let‘s Practice

lege
ol
Qs2.

C
Create a game where you start with any random game number. Ask the user to keep

na
guessing the game number until the user enters correct value.

p
A
Strings in JS

lege
l
String is a sequence of characters used to represent text

a Co
pn
Create String

let str = “Apna College“;

String Length
A
[Link]

String Indices

str[0], str[1], str[2]


Template Literals in JS
A way to have embedded expressions in strings

e
`this is a template literal`

lleg
a Co
n
String Interpolation

Ap
To create strings by doing substitution of placeholders

`string text ${expression} string text`


String Methods in JS
These are built-in functions to manipulate a string

[Link]( )

lege
[Link]( )

Col
pna
A
[Link]( ) // removes whitespaces
String Methods in JS
[Link](start, end?) // returns part of string

lege
Col
[Link]( str2 ) // joins str2 with str1

pna
A
[Link]( searchVal, newVal )

[Link]( idx )
Let‘s Practice
Qs1. Prompt the user to enter their full name. Generate a username for them based on the input.
Start username with @, followed by their full name and ending with the fullname length.

lege
l
eg: user name = “shradhakhapra” , username should be “@shradhakhapra13”

a Co
Apn
Arrays in JS
Collections of items

Create Array

lege
Col
a
let heroes = [ “ironman”, “hulk”, “thor”, “batman” ];

Apn
let marks = [ 96, 75, 48, 83, 66 ];

let info = [ “rahul”, 86, “Delhi” ];


Arrays in JS
Array Indices

arr[0], arr[1], arr[2] ....

lege
Col
pna
0 1 2
A
3 4
Looping over an Array
Print all elements of an array

lege
Col
pna
A
Let‘s Practice
Qs. For a given array with marks of students -> [85, 97, 44, 37, 76, 60]

e
Find the average marks of the entire class.

lleg
a Co
Apn
Let‘s Practice
Qs. For a given array with prices of 5 items -> [250, 645, 300, 900, 50]

e
All items have an offer of 10% OFF on them. Change the array to store final price after

leg
applying offer.

Col
pna
A
Arrays in JS
Array Methods

lege
l
Push( ) : add to end

a Co
Apn
Pop( ) : delete from end & return

toString( ) : converts array to string


Arrays in JS
Array Methods

lege
l
Concat( ) : joins multiple arrays & returns result

a Co
Unshift( ) : add to start

Apn
shift( ) : delete from start & return
Arrays in JS
Array Methods

lege
ol
Slice( ) : returns a piece of the array

na C
slice( startIdx, endIdx )

Ap
Splice( ) : change original array (add, remove, replace)

splice( startIdx, delCount, newEl1... )


Let‘s Practice
Qs. Create an array to store companies -> “Bloomberg”, “Microsoft”, “Uber”, “Google”, “IBM”, “Netflix”

lege
ol
a. Remove the first company from the array

na C
Ap
b. Remove Uber & Add Ola in its place

c. Add Amazon at the end


Functions in JS
Block of code that performs a specific task, can be invoked whenever needed

lege
Col
pna
A
Functions in JS
Function Definition Function Call

function functionName( ) { functionName( );

//do some work

lege
}

Col
pna
A
function functionName( param1, param2 ...) {

//do some work

}
Arrow Functions

ge
Compact way of writing a function

olle
a C
const functionName = ( param1, param2 ...) => {

}
//do some work

Apn
const sum = ( a, b ) => {

return a + b;

}
Let‘s Practice
Qs. Create a function using the “function” keyword that takes a String as an argument &
returns the number of vowels in the string.

lege
Col
na
Qs. Create an arrow function to perform the same task.

Ap
forEach Loop in Arrays
[Link]( callBackFunction )

CallbackFunction : Here, it is a function to execute for each element in the array

lege
l
*A callback is a function passed as an argument to another function.

a Co
[Link]( ( val ) => {

[Link](val); Ap n
})
Let‘s Practice
Qs. For a given array of numbers, print the square of each value using the forEach loop.

lege
Col
pna
A
Some More Array Methods
Map

Creates a new array with the results of some operation. The value its callback returns are
used to form new array

lege
Col
a
[Link]( callbackFnx( value, index, array ) )

Ap
let newArr = [Link]( ( val ) => { n
return val * 2;

})
Some More Array Methods
Filter

Creates a new array of elements that give true for a condition/filter.

ge
Eg: all even elements

olle
let newArr = [Link]( ( ( val ) => {

na C
})
return val % 2 === 0;

Ap
Some More Array Methods
Reduce

lege
Col
Performs some operations & reduces the array to a single value. It returns

na
that single value.

Ap
Let‘s Practice
Qs. We are given array of marks of students. Filter our of the marks of students that scored 90+.

lege
Col
na
Qs. Take a number n as input from user. Create an array of numbers from 1 to n.

p
A
Use the reduce method to calculate sum of all numbers in the array.
Use the reduce method to calculate product of all numbers in the array.

You might also like