0% found this document useful (0 votes)
31 views

Week 7 - JS Functions Extra

The document discusses JavaScript functions including function declaration, parameters, arguments, and scope. Functions allow code reusability and abstraction. Functions are declared and then called, passing arguments by value. JavaScript has three scopes: global, block, and function which determine variable visibility.

Uploaded by

alua.suleimanova
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Week 7 - JS Functions Extra

The document discusses JavaScript functions including function declaration, parameters, arguments, and scope. Functions allow code reusability and abstraction. Functions are declared and then called, passing arguments by value. JavaScript has three scopes: global, block, and function which determine variable visibility.

Uploaded by

alua.suleimanova
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CSCI 111

Web Programming and


Problem Solving
Lecture 8: JavaScript

Instructor: Talgat Manglayev and Marat Isteleyev


Table of contents

● Comparison
● Logical operators
● Functions
○ Function declaration and call
○ Arguments and parameters
○ Scope
Comparison

Greater than a > b


Lower than a < b
Greater than or equal to a >= b
Lower than or equal to a <= b
Comparison

Equal value a == b
Value and data type are equal a === b
Not equal value a != b
Not equal value or data type a !== b
Logical Operators

and (a > 0) && (a <10)


or (a > 0) || (a <10)
not ! (a == 10)
Functions
When we consume service:

- we are not interested in the way it is done;


- we can call it multiple times;
Functions
Functions parts of a program, which perform a specific task.

The functions provide:

- Abstraction: hide the internals of the code for end user; [for user]

- Reusability: use the same code multiple times in the program; [for user and developer]

- Modularity: organize and divide the program into sub tasks; [for developer]
Function declaration
function multiplyByTwo(a)

return a * 2;

}
Function call
function multiplyByTwo(a)

return a * 2;

let a = 3;

let b = multiplyByTwo(a);

console.log(b);
Function declaration
const multiplyByThree = function(a)

return a * 3;

}
Function call
const multiplyByThree = function(a)

return a * 3;

let a = 3;

let b = multiplyByThree(a);

console.log(b);
Function declaration and call comparison
function multiplyByTwo(a) const multiplyByThree = function(a)

{ {

return a * 2; return a * 3;

} }

let a = 3; let a = 3;

let b = multiplyByTwo(a); let b = multiplyByThree(a);

console.log(b); console.log(b);
Function parameters and arguments
function functionName(parameter)

{ function declaration

//do something

return something;

let a = 3; function call

let b = functionName(argument);

https://www.w3schools.com/js/js_function_parameters.asp
Function arguments

● The arguments are passed by values and not visible to


the outside of the function

● If a function changes an object property, it changes the


original value

● The functions can be passed as the arguments


Scope

Scope determines the accessibility (visibility) of variables. JavaScript has


3 types of scope:

● Global scope – visible everywhere


● Block scope – visible within a block “{}”
● Function scope – visible within a function

https://www.w3schools.com/js/js_scope.asp
Scope
Global Scope. A variable declared outside a function, have Global Scope. Global
variables can be accessed from anywhere in a JavaScript program.

Block Scope. Let and Const variables declared inside a { } block cannot be
accessed from outside the block.

Function Scope. Variables declared within a JavaScript function, can only be


accessed from within the function.

*variables assigned without var, let and const automatically have a global scope;

https://www.w3schools.com/js/js_scope.asp
Summary
Key takeaways:
• A function is block of code to perform specific task and used for: reusability,
abstraction and modularity
• Remember 2-step function usage: declare and call
• The arguments are passed by value, though object and array arguments can be
changed inside a function
• JavaScript has 3 types of scope: block, function, global
• Visibility of variables differs depending on scope

You might also like