Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

02 JavaScriptBasics

This document provides an overview of JavaScript basics including variables, scopes, closures, expressions, operators, and statements. Key points: - Variables are declared with var and can change types but not be deleted. Values are garbage collected if unreachable. - Scopes are delimited by functions. Global and local scopes exist, as well as scope chains that link nested functions. - Closures allow functions to access variables from outer scopes even after outer functions have closed. They enable private state and are used to create unique IDs. - Expressions include literals and operators which have precedence. Common operators include arithmetic, comparison, logical, and assignment. - Common statements include if/else, do/while, for

Uploaded by

Andrei Ursuleanu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

02 JavaScriptBasics

This document provides an overview of JavaScript basics including variables, scopes, closures, expressions, operators, and statements. Key points: - Variables are declared with var and can change types but not be deleted. Values are garbage collected if unreachable. - Scopes are delimited by functions. Global and local scopes exist, as well as scope chains that link nested functions. - Closures allow functions to access variables from outer scopes even after outer functions have closed. They enable private state and are used to create unique IDs. - Expressions include literals and operators which have precedence. Common operators include arithmetic, comparison, logical, and assignment. - Common statements include if/else, do/while, for

Uploaded by

Andrei Ursuleanu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

JavaScript Basics

Variables

Explicitly declared with var: var x=0, y=1, z=2;

Variables are untyped can change their value to different types


Unlike properties, cannot be deleted Undefined vs. unassigned

Automatic garbage collection of unreachable values


Circular references and memory leaks

Scopes

Global and local scopes

No code block scopes (e.g. inside if, for, while) window and global scope
Frames, multiple global scopes and security concerns

Scopes are delimited by functions


Scopes are chained Functions are invoked in the scope they were defined new Function() constructor scope Calling a function will add a call object (activation object) to the scope chain, having arguments as property

Scope Chains

Closures

Functions can be nested


Examples of closure:

function A() { var a = 'A scope'; function B() { var b = 'B scope'; alert(a); } B(); alert(b); } var uniqueID = (function() { var id = 0; return function() { return id++; }; })();

Using closure to create scope bind

Expressions and Operators


Expressions: literals and operators

Operators have precedence and associativity (unary, assign, ternary are right-to-left)
Arithmetic operators: +, -, *, /, %, ++, --

Equality and identity: == and ===, != and !==


Relational operators: >, <, >=, >=, in, instanceof String operators: +, >, <, >=, >= (alphabetical comparison)

Logical operators: &&, ||, !


Bitwise operators: &, |, ^, ~, <<, >>, >>> Assignment: =,
+=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^=

Other operators: ?:

typeof new delete void . , ()

Statements

Statements we already know: if, else, do/while, switch, for, for/in, break, continue with statement Empty statement ; switch also works with string values

Recap Scopes and Closures

You might also like