JS, Understanding Weird parts-1 (sec 2 & 3)

Section/Dir: 2

Syntax parsers: A program that reads your code and determines what it does and if its grammar is valid.

Lexical environment: Where something sits physically in the code you write. Lexical means ‘having to do with words or grammar’. It means where we write our code is important.

Execution context: A wrapper to help manage the code running.

In Javascript object is just a name-value pair and value itself maybe another name-value pair.

Single Threaded: means execution of one command at a time.

Synchronous: means one at a time.

javascript does single threaded synchronous execution.

  1. At the global level, “this” refers to “window” Object. When we say global, that means we are not inside any function. Which var and funcs are not inside any function are directly attached to window object.
  2. undefined” is a special value that tells us that a variable is declared but value is not set yet.
  3. every time a function is invoked, a new execution context is created and vanishes when the function returns.

Note: Browser puts things asynchronously but JS engine runs synchronously

// long running function
function waitThreeSeconds() {
    var ms = 3000 + new Date().getTime();
    while (new Date() < ms) {
    }
    console.log('finished function');
}

function clickHandler() {
    console.log('click event!');
}

// listen for the click event
document.addEventListener('click', clickHandler);

waitThreeSeconds();
console.log('finished execution');


/* OUTPUT/Console*/

// finished function
// finished execution
// click event!

Here the click event handler run at last.


 

Section/Dir: 3

Dynamic Typing: We dont tell what type of data it stores in variables, figures it out while the code is running or executing. So, a single var at different time can hold different types of values. For other languages, they uses static typing, i.e. we tell what type of data we want to store in a var.

Primitive type: A type of data that represents a single value.

js data types: Six primitive types of data

1) undefined: lack of existence. Its what js sets to variable until we set a value for it.

2) null: When we want to set nothing for a variable.

3) boolean

4) number: one number type only. Its floating point number.

5) string

6) symbol: used in ES6 (not supported in old browsers..)

Operator precedence and associativity:

The equal operator (all operators are infix functions) ‘=’ has right to left associativity. So, if JS sees two ‘=’ operator in a line, the right one will be executed first. The ‘=’ operator also returns a value.

a = 4;

This assignment returns the value of right side component.

A cool example about associative & coercion:

console.log( 3 < 2 < 1 ); // TRUE .. why?

/*
 * '<' is left associative thats why the left one will
 * be executed first, which will return false. Then 
 * the statement becomes false < 1. Then 0 < 1.
 * 
 */

// We can use Number(values) to test the coercion behaviour

Number(false) // 0
Number(true) // 1
Number(undefined) // NaN
Number(null) // 0

Using || for fallback system,

// '||' OR operator returns the value which can be coerced to TRUE

console.log( undefined || "hello" )		// "hello"
console.log( null || "hello" )			// "hello"
console.log( "" || "hello" )			// "hello"

completed…

Leave a Reply

Your email address will not be published. Required fields are marked *