Java Notes
Java Notes
variables:are used to store the value in js nd can be defined using var , let or
const key words
data types: js supports severl data types incliding numbers string bollen array
objects and null
function are the block code that can be reused mult tyms
can be defined using the fumction kayword and can take parameters as input and
return val as op
variables in js :
1) var hello=10;
2) let hello=10;
3) const hello=10;
comments in js:used to exp the code and meke it more readable, for commenting use//
comparision operators: are ues to compare two values and return a bollean value
1)=:returns true if both the values are equal
2)not equal : returns true if both the values are not equal
3)> than : returns true if the value one is> tha value two
4)< than : retrns true if value one is < than value 2
5)>= : retruns true if the value on eis> than or= to value two
6)<=:
Bitwise operator: are used to perform the operations on the binary no.s bitwise
operator only performs the operatons on the integers the integers values are
converted into the binary then the operation is performed and the binaary is
converted back into the integer.
1) bitwise and (&) : it will return 1 if both the coreesponding bits of both
operands 1
2) bitwise or =(|) it will return 1 if any of the either oprend
3) xor: returns 1 in each bit position for which the correspondiing bits are 1 but
not both operands are 1
4) bitwise not: inverts the bits of opreand
5) left shift : shifts the bit of first operand to the left by the no. of positions
specified by the second operand
6) right shift : shifts the bit of first operand to the right by the no. of
positions specified by the second operand
use cases of js
1 to handel the events like button clicks ,mouse movement , keyboard inputs
2) to manupulate the html elements ,we can use this to change the content and
styling of html element
ex: we can change the text inside the paragraph or we can chng the bgclr or txt
clr.
3) validating the user inputs
4) we can use this for creating the annimations, for performing the calculations
js events :
js objects :
objects are similar to the variables but objects can contain multiple values
ex:- const bike={type:"bullet","color:red","model:2012"};
objects: an object is a collection of key value pair where the kwys are the string
and the values can be any data type
when we have to execute a block of code on the basis of some condition we can use
the if ststement , if else statement ,nested if statement
the condition is the logic that u want to test , if the condition will pass it will
return the value if true ,if the condition fail the vakue if fails
if( condition ) {
//block of code//
}
if condition{
// block of code if condition is true
}
else{
// block of code if condition is false
}
var age = 50
if (age=>50){
console.log ("elder")
var age = 50
if (age=>50){
console.log ("elder")
}
else if (age>10 && age <16 ){
console.log("kid")
}
else if (age>18){
console.log("adult")
}
else{
coonsole.log("invalid age")
switch case
the script statement and js is a controling structure that allows u to specify or
exescute a differnt block of code depending upon a specific value it is used as an
alternative to if else ladder when u have multiple condition to chgeck against a
single value
sysyntax
let x=tania;
switch(x){case "tania"
the break staement is used to exit the switch and prevent the code from being
executed
the default statement is optional and is executed when there are no expression
matches or no case value match then it will return the content of the default
statement
ternary operators
loops :
loops are a common way to control the flow of aprogram that allows u to repeat a
block of code for specific number of tym
a for loop is a control flow ststement used for executing a block ogf code for a
specific no. of tyms
it contains 3 components
1 initialization : it sets the starting point
2 condition : the condtion defines the termination condition
3 increment / decrement : updates the loop control variable
for loops are usedc when u know the exact no. of itteration required
while loop
it is used for execcuting a block of code as long as a certain condition is true
it only has a condition that check before each itteration
it the condition is true the loop body is executed and the process contnied until
the condition become false
while loops areful when u dont know for how many tyms the loop is to be executed
for(initializtaion; condition;inc/dec)
example : for (let i=0; i<5; i++){console.log(i);}
syntax :while loop
while(cond){
}
programs
1 write a for loop that prints the no. 1 to 10
2write a for loop that prints a even no from 2 till 20
3 write a for loop that calculate the sum of no. from 1 to 100 the result must be
stored in a variable called total sum
4 write a for loop that prints a reverse order of no.s from 10to 1
5 write a for loop that prints a squre root of nos.1to 10