# Scope
# Local Variables
function a {
var x = 2;
console.log(x);
}
// 2
function a {
var x = 2;
}
console.log(x)
// undefined
2
3
4
5
6
7
8
9
10
11
12
13
14
These are called local variables. And they are available at their local scope.
# Global Variables
var x = 2;
function a {
console.log(x)
}
// 2
if (true){
var x = 2
console.log(x)
}
//2
if (true){
var x = 2
}
console.log(x)
//2 This still works because getting declared inside `if` or `while` is available as a global variable.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# var let const
const is immutable, var and let is mutable.
All three of them are Local/Global depending on where they are declared.
When created inside
if/else
for
while
statements,var
is global.let
const
is local
Much safer and more predictable to use
let
instead ofvar
. Very few scenarios requirevar
to be used.
# var
- variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top.
- hoisting will affect the variable declaration, but not its value's initialization
- If no value -> undefined. If no declaration -> ReferenceError
function do_something() {
console.log(bar); // undefined
var bar = 111;
console.log(bar); // 111
}
// ...is implicitly understood as:
function do_something() {
var bar;
console.log(bar); // undefined
bar = 111;
console.log(bar); // 111
}
var x = y, y = 'A';
console.log(x + y); // undefinedA
//x and y are declared before any code is executed, but the assignments occur later
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# let
# const
# Strict mode
Default, non-strict mode referred to as sloppy mode. introduced in ECMAScript 5, is a way to opt in to a restricted variant of JavaScript. Support may differ per browser.
# Purpose
- Eliminates some JavaScript silent errors by changing them to throw errors.
- Can sometimes be made to run faster than identical code that's not strict mode.
# Usage
- Strict mode applies to entire scripts or to individual functions. It doesn't apply to block statements enclosed in
{}
braces. 'use strict'
# Converting mistakes into errors
Strict mode changes some previously-accepted mistakes into errors. JavaScript was designed to be easy for novice developers, and sometimes it gives operations which should be errors non-error semantics.
Strict mode makes it impossible to accidentally create global variables.
Any assignment that silently fails in normal code (assignment to a non-writable global or property, assignment to a getter-only property, assignment to a new property on a non-extensible object) will throw in strict mode.
Delete undeletable properties throw (where before the attempt would simply have no effect).
'use strict'; delete Object.prototype; // throws a TypeError
1
2Function parameter names be unique
function sum(a, a, c) { // !!! syntax error 'use strict'; return a + a + c; // wrong if this code ran }
1
2
3
4Forbids octal syntax. Novice developers sometimes believe a leading zero prefix has no semantic meaning, so they use it as an alignment device — but this changes the number's meaning!
// ocatal syntax supported by browsers (but not by ECMAScript) 0644 === 420. // In ECMAScript 2015 Octal number is supported by prefixing a number with "0o" var a = 0o10; // ES2015: Octal
1
2
3
# Simplifying variable uses
Strict mode prohibits
with
.eval
of strict mode code does not introduce new variables into the surrounding scope.Forbids deleting plain names.
delete name
in strict mode is a syntax errordelete x; // !!! syntax error
1
# eval and arguments are simpler
I don't understand this part yet.
# Securing Javascript
Users can change JS and access private information. To censor access to forbidden functionality, many runtime checks must be done. But this has a considerable performance cost. A few strict mode tweaks, plus requiring that user-submitted JavaScript be strict mode code and that it be invoked in a certain manner, substantially reduce the need for those runtime checks.
- No longer possible to reference the
window
object throughthis
inside a strict mode function fun.caller
is the function that most recently calledfun
, andfun.arguments
is thearguments
for that invocation offun
. Iffun
is in strict mode, bothfun.caller
andfun.arguments
are non-deletable properties which throw when set or retrieved.
# runtime
# Block
The block statement is often called compound statement in other languages. It allows you to use multiple statements where JavaScript expects only one statement. Commonly used in association with
if...else
(opens new window) andfor
(opens new window) statements.
Variables declared with
var
or created by function declarations in non-strict mode do not have block scope. Variables introduced within a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself.By contrast, identifiers declared with
let
(opens new window) andconst
(opens new window) do have block scopeconst
const c = 1; { const c = 2; } console.log(c); // logs 1 // and does not throw SyntaxError: Identifier 'c' has already been declared // because it can be declared uniquely within the block
1
2
3
4
5
6
7
8
# Window object
# This
t
# call
# Hoisting
Variables declared using
var
are created before any code is executed in a process known as hoisting
# Destructuring assignmnet
let a, b, rest;
[a, b] = [10, 20];
console.log(a);
// expected output: 10
console.log(b);
// expected output: 20
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest);
// expected output: Array [30,40,50]
2
3
4
5
6
7
8
9
10
11
12
13
# Hash and Rainbow
https://security.stackexchange.com/questions/379/what-are-rainbow-tables-and-how-are-they-used
https://cryptii.com/
https://www.youtube.com/watch?v=G2_Q9FoD-oQ
https://www.youtube.com/watch?v=V4V2bpZlqx8
# DB
https://kkovacs.eu/cassandra-vs-mongodb-vs-couchdb-vs-redis