JavaScript is a dynamic computer programming language .it is lightweight and most commonly used as a part of web pages and it is the scripting & interpreted programming language.
2. Variable:
a variable used to store information ,that value can change in the computer program
if we want declare a variable we just remember 5 thinks first write the key word var or let or const and second give the name of variable third =sign fourth given variable value name and finally give semicolon ;
There are Three type of variable
- string type
- number type
- Boolean type
if we want to know every variable which type we can identify easily
3. Mathematical operation in JavaScript
- (+)Addition
- (-)Subtraction
- ( *)Multiplication
- ( ** )Exponentiation (ES2016)
- ( / )Division
- ( % )Modulus (Remainder)
- ( ++ )Increment
- — Decrement
4 . Array
In JavaScript, array is a single variable that is used to store different elements.
how to declare array
var friendName=[“Amin”,“Tareq”,“Farhan”,“Sumon”,“Anik”,“Ashik”,“Babul”];
1 . at the beginning if we want to see the array value in ouput it’s very easy just use the array index like
console.log(friendeName [2]);
output will be : Farhan
2 . if we want to change value inside array we can write a simple way
friendName[3]=“Nabil”;
console.log(friendeName );
output will be :[“Amin”,“Tareq”,“Farhan”,“Nabil”,“Anik”,“Ashik”,“Babul”]
Sumon will be change.
3. if we want to find out index or position in array
var position=friendName.indexof(“Babul”);
console.log(positon);
output will be :6
4. but if we write
var position=friendName.indexof(“Mamun”);
which is not define in array output will be -1 because it’s not include in array
console.log(positon);
output will be :-1
5 .Array value add in last
just write
friendName.push(“Anika”);
output will be :[“Amin”,“Tareq”,“Farhan”,“Nabil”,“Anik”,“Ashik”,“Babul”,“Babul”,“Anika”];
6 .Array value add in first
just write
friendName.unshift(“Foysal”);
output will be :[ “Foysal”,“Amin”,“Tareq”,“Farhan”,“Nabil”,“Anik”,“Ashik”,“Babul”,“Babul”,“Anika”];
7 . Array value Delete at beginning
friendName.shift(“Foysal”);
output will be :[“Amin”,“Tareq”,“Farhan”,“Nabil”,“Anik”,“Ashik”,“Babul”,“Babul”,“Anika”];
Foysal gone
8. In array also do slice
var number=[10,11,12,13,14,15,16];
var part1=number.slice(3,5);
output well be : [13,14];
remember that in using slice main array do not change.
9. In array also do splice
remember that in using slice main array will be change.
5 .Loop in JavaScript
1.why use loop
same think do again and again that’s why use loop
2. In JavaScript loop two type mostly use
- for loop
- while loop
for loop
for (variable declaration ;condition ; increment/decrement){
statment;
}
example :
var text = “”;
var i;
for (i = 0; i < 5; i++) {
text += “The number is “ + i + “<br>”;
}
while loop
while (condition) {
// code block to be executed
}
example :
while (i < 10) {
text += “The number is “ + i;
i++;
}
6 . Function
in JavaScript most useful and important thinks is function.
- What is function
function is a set of statement group of reusable code which can be called anywhere in your program.
Example of function:
function add(num1,num2){
var result=num1+num2;
return result;
}
var sum=add(10,20);
console.log(sum);
output will be : 30
num1 & num2 is a function parameter
and return is a keyword it’s duty is stop this statement
2.What is different between loop and function
- loop give output continuously do not stop give all the output until condition fulfil
- but function do not do this when user want this place show output it given anywhere user it call
8 . Factorial using function
function factorial(n){
var factorial =1;
for(var i=1;i<n; i++){
factorial=factorial*i;
}
return factorial;
}
var result=factorial(10);
console.log(result);
output will be :3628800
9. var/let/const
var
- it al time can use
2. var is a function scope inside the function area it can call
example:
if(true){
var myVariable=“This is var”;
}
console.log(myVariable);
let
- if have possibility to change the variable name then use the let l
example :
let patient=20;
patient=40;
if(true){
let letVariable=“this is let”;
console.log(letVariable);
}
2. let is block scope it only work in block area not other
const
1 .if variable value do not have to chance to change in next that time use the
const.
2. const fix no change
example:
const country=“Bangladesh”;
console.log(country);
10. Object in JavaScript
- What is object?
object is a collection of properties or method and property is an association between a name(or key) and a value
2.object declaration
var student1={name:“Babul”,id:18103045, phone:01725488};
var student2={name:“Kamrul”,id:19103045, phone:01925488};
console.log( student1);
{name:“Babul”,id:18103045, phone:01725488}
console.log( student2);
{name:“Kamrul”,id:19103045, phone:01925488}
3. if anybody wants to see only phone number
var phoneNo=student1.phone;
output :01725488;
4.if anybody wants change to phone number
student1.phone=0162015;
output:{name:“Babul”,id:18103045, phone:0162015}