Course Hero Logo

Week3_Recap.pdf - CSD 122 - Week 3 Recap Paul Mai...

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 1 out of 20 pages.

Unformatted text preview: CSD 122 - Week 3 Recap Paul Mai Assistance - Darren Stults Variable Scope 1. 2. 3. 4. Variables declared outside a function are global x = 5; // no keyword => Scope: implicit global even if declared inside function var x; => Can be called, before declaring (hoisting) (but returns undefined) let y; => Must be declared before it is called Good JavaScript Programming Style - Always use let keyword when declaring a variable in JS. Variable & Global scope - Without using keyword, a variable scope, which is declared inside a function, is also global Variable Redeclaration Using var or no keyword, a variable can be redeclared again and again Using let, a variable cannot be redeclared again Strict vs Loose Equality - Javascript supports both strict (===, !==) and loose (==, !=) equality operators. - Always use strict equality operators unless you know exactly what you are doing. Conditional Statements (Best practices) ● Syntactically, using else statements is optional. ● Logically speaking, always try to include else statements in your program to capture unknown cases Functions Three important characteristics of functions: ● Decomposition ● Abstraction ● Encapsulation Benefits: - Break the problem into smaller and manageable problems - Hide detailed implementation - Make the code reusable - Need test only once JavaScript and Functions - Functions are first-class objects - Functions are treated like any other JavaScript object - When function is out of scope, function variables cease to exist - Similar to an object, when an object is null, all properties cease to exist - When a function is called, all variables will have default initial values - Similar to an object, when an object is created, all properties have default values - Functions can be referenced by variables - Functions can be declared with literals or function definition - Functions can be passed as arguments to other functions Checkpoint What is wrong with the following statement? let x = 10; console.log(x); let x = 9; What does the following statement print? let y = 6; let z = ‘6’; if (y == z) console.log(y + z); else console.log(z); Checkpoint What does the following statement print? console.log(x); var x = 9; What does the following statement print? console.log(x); let x = 9; Does the following statement run? const display = () => x = 7; display(); console.log(x); Summary - Do not create global variables, use local variables instead. Always use let keyword instead of var when declaring a variable Better still: use const unless you need to rebind that variable Always use strict equality operators (===, !==) when comparing two variables. - If possible, transition from traditional function declaration style to expression style. Data Types in JavaScript JavaScript supports the following data types: Primitive Types ● ● ● ● ● ● Number String Boolean Symbol (new in ECMAScript 6) Null Undefined Object / Reference Types ● ● ● ● Function Array Date RegExp Memory Management ● ● ● ● ● Two types of memory segments: Stack and Heap Stack / (Call Stack) tracks where a program is during runtime JS runtime V8: Heap is used to store values of primitives and object/reference types ○ Primitive data types are immutable ○ Object/reference types are mutable Runtime engine memory errors: ○ StackOverflowError - The stack ran out of space ○ OutOfMemoryError - The heap ran out of memory JS runtime has a garbage collector to clean heap memory ○ runs periodically in the background Stack vs Heap memory geeksofgeeks.com Example Example Example int a = 77; Person person = new Person(); JS Runtime Stack Function main(args) { console.log X=5 Y=6 y=6 function multiply(x, y) { x=5 args = function main(args) { a = 6; x = ; y = [‘test1’, ‘test2’]; modify(y); Stack ; Heap x0002 String X0003 X0004 test1 x0003 test2 x0004 z=x0002 y=x0002 x=x0001 a=6 x0001 Int[2] 0,0??? test3 x0005 Checkpoint function main(args) { a = 6; x = ; y = [‘test1’, ‘test2’]; modify(y); } function modify(arr) { arr[0] = ‘test3’; } What would be the result of the previous console.log statement? a. test1, test2 b. test3, test2 ...
View Full Document

Newly uploaded documents

Show More

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture