Skip to content

Commit 5739e57

Browse files
committed
Function
1 parent 99ea27a commit 5739e57

File tree

2 files changed

+329
-0
lines changed

2 files changed

+329
-0
lines changed

09. Function/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<h1 id="showMessage"></h1>
10+
<!-- link javacript file -->
11+
<script src="js/script.js"></script>
12+
13+
</body>
14+
</html>

09. Function/js/script.js

Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
// A JavaScript function is a block of code designed to perform a particular task.
2+
// A JavaScript function is executed when "something" invokes it (calls it).
3+
4+
// JavaScript Function Syntax
5+
// A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().
6+
// Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).
7+
// The parentheses may include parameter names separated by commas:
8+
// (parameter1, parameter2, ...)
9+
// The code to be executed, by the function, is placed inside curly brackets: {}
10+
11+
// function name(parameter1, parameter2, parameter3) {
12+
// // code to be executed
13+
// }
14+
15+
// Function parameters are listed inside the parentheses () in the function definition.
16+
// Function arguments are the values received by the function when it is invoked.
17+
// Inside the function, the arguments (the parameters) behave as local variables.
18+
19+
// Function Invocation
20+
// The code inside the function will execute when "something" invokes (calls) the function:
21+
// When it is invoked (called) from JavaScript code
22+
// Automatically (self invoked)
23+
24+
// Function Return
25+
// When JavaScript reaches a return statement, the function will stop executing.
26+
27+
// If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement.
28+
29+
// Functions often compute a return value. The return value is "returned" back to the "caller":
30+
31+
32+
function Greeting() {
33+
console.log("Hello, Aktar!");
34+
}
35+
36+
Greeting();
37+
38+
function Greeting1(name) {
39+
console.log("Hello,", name + "!");
40+
}
41+
Greeting1("Shakib");
42+
43+
// Covert Fareheit to Celcius
44+
45+
function toCelcius(fareheit) {
46+
return (5 / 9) * (fareheit - 32);
47+
}
48+
49+
console.log("Celcius: " + toCelcius(24));
50+
51+
// we can also call function and store it value to variable:
52+
let value = toCelcius(50);
53+
console.log("Celcius: " + value);
54+
55+
// Local Variables
56+
// Variables declared within a JavaScript function, become LOCAL to the function.
57+
// Local variables can only be accessed from within the function.
58+
// function myFunction() {
59+
// let carName = "Volvo";
60+
// // code here CAN use carName
61+
// }
62+
63+
// console.log(carName); // carName is a local variable of myFunction we can not access it out of the function
64+
65+
66+
//We can call the function after or before the function definition. But need to fuction must be defined
67+
68+
// It happens because javascript has technology which is hosting. Hosting hold up the declaretion of javascript let, var, function to to top
69+
// Before running the program
70+
71+
myFriend();
72+
function myFriend() {
73+
console.log("What's up bro? What can do for you?");
74+
}
75+
76+
// Note: Arrow Function call should be happen after the function definition
77+
78+
// we can also defined default value of the function parameter
79+
function fullName(firstName = "Abullah", lastName = "Abdur Rahman") {
80+
console.log(firstName, lastName);
81+
}
82+
fullName();
83+
fullName("Abdullah", "Shafiq");
84+
85+
// JavaScript Arrow Function
86+
// Arrow functions were introduced in ES6.
87+
// Arrow functions allow us to write shorter function syntax:
88+
let a = 4;
89+
let multipliedTwo = (a, b) => a * b;
90+
console.log(multipliedTwo(23, 4));
91+
92+
// If there is only one parameter of arrow function you can skip the parenthesis
93+
// Otherwise we must be put the parethesis ()
94+
95+
let morningGreeting = name => console.log("Good Morning:", name);
96+
morningGreeting("Aktaruzzaman");
97+
98+
// It gets shorter! If the function has only one statement, and the statement returns a value, you can remove the brackets and the return keyword:
99+
100+
let loudAndClear = sound => sound + " Allahu Akbar!";
101+
console.log(loudAndClear("Favourite Sound:"));
102+
103+
// we cab also add one more statement in arrow function
104+
let hello = () => {
105+
console.log("Hello, ");
106+
console.log("Bangladesh! ");
107+
}
108+
109+
hello();
110+
111+
// Array Iteratin using forEach
112+
var foods = ["Cake", "Ice cream", "Chocolate", "Bread"];
113+
var numbers = [1, 3, 4, 5, 67, 8, 9];
114+
115+
// 1st parameter : Item
116+
// 2nd parameter : Index
117+
// 3rd parameter : Whole array
118+
119+
foods.forEach(function(value, index, arr){
120+
console.log(value);
121+
console.log(arr);
122+
console.log(index);
123+
});
124+
125+
// we can also access only value of array, If this function have one parameter this is the item, if two first one is item second one is index, if three last one is whole array
126+
127+
numbers.forEach(function(value){
128+
console.log(value);
129+
});
130+
131+
console.log("Map:")
132+
// Map array Method
133+
const newArr = numbers.map(function(value){
134+
return value * value;
135+
});
136+
137+
console.log(newArr);
138+
139+
// Method in Object
140+
141+
let ob1 = {
142+
fname: "Abdullah",
143+
lname: "Abdur Rahman",
144+
age : 18,
145+
village : "Abdullapur",
146+
fullName : function(){
147+
return `${this.fname} ${this.lname}`;
148+
}
149+
}
150+
151+
console.log(ob1.fullName());
152+
console.log(ob1.lname);
153+
154+
//Function with in function
155+
Explanation:
156+
// myName("Aktaruzzaman"): When you call myName("Aktaruzzaman"), it returns the retName function.
157+
// myName("Aktaruzzaman")(): The returned function retName is then immediately invoked by the () after it, which returns the value of name, i.e., "Aktaruzzaman".
158+
function myName(name){
159+
function retName(){
160+
return name;
161+
}
162+
return retName;
163+
}
164+
165+
console.log(myName("Aktaruzzaman")());
166+
167+
168+
// Math Object
169+
170+
let val;
171+
val = Math.PI
172+
console.log(val)
173+
console.log(Math.E)
174+
175+
console.log(Math.round(5.5));
176+
console.log(Math.ceil(5.01));
177+
console.log(Math.floor(5.99));
178+
console.log(Math.sqrt(9));
179+
console.log(Math.abs(-33));
180+
console.log(Math.pow(2,3));
181+
console.log(Math.min(23,32,3,42,-1));
182+
console.log(Math.max(43,4,23,21));
183+
184+
// The Math.random() function in JavaScript produces a floating-point number between 0 (inclusive) and 1 (exclusive). This means that:
185+
186+
// The smallest possible value returned by Math.random() is 0 (though it's extremely rare to actually get 0).
187+
// The largest possible value is just slightly less than 1, such as 0.9999999999999999, but it will never be exactly 1.
188+
189+
console.log(Math.random());
190+
console.log(Math.floor(Math.random() * 100 + 1));
191+
192+
193+
// Date Object
194+
let today = new Date();
195+
console.log(today);
196+
197+
console.log(new Date("11-05-1998"));
198+
let birthdate = new Date("04-13-1998");
199+
console.log(birthdate);
200+
201+
console.log(new Date("04-13-1998 5:10:11"));
202+
console.log(new Date("04/13/1998 5:10:11"));
203+
204+
205+
//Month and Day start with 0
206+
console.log(new Date().getMonth());
207+
console.log(new Date().getDay());
208+
//Fetch the date
209+
console.log(new Date().getDate());
210+
console.log(new Date().getFullYear());
211+
212+
// getTime() function returns amount of seconds since jan 1, 1970
213+
console.log(new Date().getTime());
214+
215+
216+
console.log(new Date().getHours());
217+
console.log(new Date().getMinutes());
218+
console.log(new Date().getSeconds());
219+
console.log(new Date().getMilliseconds());
220+
221+
// we can set month, date,year, hour etc in eixisting Date obejet
222+
birthdate = new Date("04-13-1998");
223+
birthdate.setMonth(5);
224+
console.log(birthdate);
225+
226+
// JavaScript Scope
227+
228+
// Scope determines the accessibility (visibility) of variables.
229+
// JavaScript variables have 3 types of scope:
230+
// Block scope
231+
// Function scope
232+
// Global scope
233+
234+
// Block Scope
235+
// Before ES6 (2015), JavaScript variables had only Global Scope and Function Scope.
236+
// ES6 introduced two important new JavaScript keywords: let and const.
237+
// These two keywords provide Block Scope in JavaScript.
238+
239+
240+
// Variables declared with let and const inside a { } block cannot be accessed from outside the block:
241+
242+
{
243+
let x = 2;
244+
console.log(x);
245+
}
246+
247+
// console.log(x); // cause a error
248+
249+
// Variables declared with the var keyword can NOT have block scope.
250+
// Variables declared inside a { } block can be accessed from outside the block.
251+
252+
{
253+
var x = 4;
254+
console.log(x);
255+
}
256+
console.log(x);
257+
258+
// Function Scope
259+
// JavaScript has function scope: Each function creates a new scope.
260+
// Variables defined inside a function are not accessible (visible) from outside the function.
261+
// Variables declared with var, let and const are quite similar when declared inside a function.
262+
263+
// They all have Function Scope:
264+
function myFunction() {
265+
var carName = "Volvo"; // Function Scope
266+
}
267+
// console.log(carName); //erros
268+
function myFunction() {
269+
let cNamee = "Car"; // Function Scope
270+
}
271+
// console.log(carName); //error
272+
function myFunction() {
273+
const crName = "Bus"; // Function Scope
274+
}
275+
// console.log(crName);//error
276+
277+
278+
// Global JavaScript Variables
279+
let carName = "Volvo";
280+
281+
// code here can use carName
282+
function myFunction() {
283+
console.log(carName); //error
284+
}
285+
286+
console.log(carName);
287+
myFunction();
288+
289+
// Automatically Global
290+
// If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.
291+
// This code example will declare a global variable carName, even if the value is assigned inside a function.
292+
293+
myFunction();
294+
295+
// code here can use carName
296+
297+
function myFunction() {
298+
carName = "Auto Global";
299+
}
300+
301+
console.log(carName);
302+
303+
304+
// Strict Mode
305+
// All modern browsers support running JavaScript in "Strict Mode".
306+
// In "Strict Mode", undeclared variables are not automatically global.
307+
308+
309+
function myStrict(){
310+
"use strict";
311+
hen = 2; // casuse error
312+
}
313+
314+
// myStrict();
315+

0 commit comments

Comments
 (0)