Java script notes
What is JavaScript?
JS is a programming language. We use it to give instructions to the
computer.
way of output:
1. [Link]();
2. Alert();
Way to input:
prompt()
Ex.
const userInput = prompt("Please enter your name:");
Variable Rules:
1. Variable names are case sensitive; “a” & “A” is different.
2. Only letters, digits, underscore( _ ) and $ is allowed. (not even
space) Only a letter, underscore( _ ) or $ should be 1st character.
3. Reserved words cannot be variable names. Apna College.
let, const & var :
1. Var: Variable can be re-declared & updated. A global scope
variable.
[Link] : Variable cannot be re-declared but can be updated. block
scope variable.
[Link] : Variable cannot be re-declared or updated. A block scope
variable.
Comments in JS Part of Code which is not executed:
1. // for single line comment.
2. /* */ for multiple line comment.
Operators in JS Used to perform some operation on data
[Link] Operators: +, -, *, /
[Link] Operators: +=, -=, *=, %=, *=.
[Link] Operators: Equal to == .Not equal to != >, >=, <,
<= Equal to & type Not equal to & type === !==
3. Logical Operators: Logical AND: &&
Java script notes
4. Logical OR : ||
5. Logical not: !
Conditional Statements:
To implement some condition in the code
1. if statement.
2. else statement.
3. If-else statement.
Loops in js:
Loops are used to execute a piece of code again & again
1. for Loop:
for (let i = 1; i <= 5; i++)
{ [Link]("apna college"); }
2. while Loop:
while (condition)
{ // do some work }
3. Do-while loop:
do
{ // do some work }
while (condition).
4. for-of Loop :
for (let val of strVar)
{ //do some work }
[Link]=[12,34,23,12,34];
5. for-in Loop:
for (let key in objVar)
{ //do some work }
Java script notes
Strings in JS:
String is a sequence of characters used to represent
text.
Create String :
let str = “Apna College“;
String Length: [Link].
String Indices: Apna College str[0], str[1], str[2].
Template Literals in JS:
A way to have embedded expressions in strings.
`this is a template literal`
String Interpolation :
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.
1. [Link]( )
2. [Link]( )
3. [Link]( ) // removes whitespaces Ap
4. [Link](start, end?) // returns part of string
5. [Link]( str2 ) // joins str2 with str1
6. [Link]( searchVal, newVal )
7. [Link]( idx )
Java script notes
Arrays in JS:
Collections of items Create Array
[Link] heroes = [ “ironman”, “hulk”, “thor”, “batman” ];
[Link] marks = [ 96, 75, 48, 83, 66 ];
[Link] info = [ “rahul”, 86, “Delhi” ];
Array Methods:
1. Push( ) : add to end
2. Pop( ) : delete from end & return
3. toString( ) : converts array to string
4. Concat( ) : joins multiple arrays & returns result
5. Unshift( ) : add to start
6. shift( ) : delete from start & return
7. Slice( ) : returns a piece of the array slice( startIdx, endIdx )
8. Splice( ) : change original array (add, remove, replace)
splice( startIdx, delCount, newEl1…).
Functions in JS: