Variables in Javascript

Javascript is type inferenced which mean based on the value it will identify the type and hence we dont have to explicitly specify the type.

There are 3 primitive data types and undefined, null

  • Number: These are double precision 64 bit format (there are no Integers, Short, Float so on)
  • String: Sequqnces of Unicode Characters (no char type)
  • Boolean: It is of type ‘Boolean’ with two possible values ‘true/false’
  • undefined: It is of type ‘undefined’ with two possible values ‘undefined’. Value assigned to every declared variable until its defined.
    • Ex var value; value = 10; //value between these two statements is value ‘undefined’ of type ‘undefined’
  • null: It is of type ‘null’ with two possible values ‘null’.
  • In ECMA6, new variable called Symbol is introduced just like ENUMs

Note: There is no scoping information attached to variable declarations and hence all declared variables are by default global.

Variables and Values can be interrogated using ‘typeof’

typeof <variable>
typeof <value>
var a;
console.log(typeof a);
a = 10;
console.log(typeof a);
a = "hello";
console.log(typeof a);
a = true;
console.log(typeof a);
a = null;
console.log(typeof a);

Null is a typeof Object
* a=null; typeof a – returns object instead of null, it was a bug in early versions of JS but its not fixed in newer versions bcos it breaks backward compatibility and many web applications will break.

Type Coercion: As JS was introduced to be friendly  language with developers and they did type conversion of variables used in the expression and this lead to many confusions and later they fixed it but because of backward compatibility we still live with them. One such issue is ‘==’

  • double equals == and triple equals ===
  • 12 + “4” -> results in “124” because it looks at expression and finds one is string and other is number and hence coerce number into string so that it can do string concatenation
    * JS does a lot of type coercion and hence the behavior is unpredictable lot of times, beware of it
var a = 10;
var b = "10";
a == b -> returns true, whereas
a === b -> returns false

Values of all types have associated boolean value

  • Non zero numbers can be passed to a if loop which returns true
  • Non empty strings can be passed to a if loop which returns true
  • undefined and null are always false
var a = 10;
if(a) {
console.log("a is true");
} else {
console.log("a is false");
}

a = 0;
if(a) {
console.log("a is true");
} else {
console.log("a is false");
}

a = "Hello";
if(a) {
console.log("a is true");
} else {
console.log("a is false");
}

a = "";
if(a) {
console.log("a is true");
} else {
console.log("a is false");
}

Objects in Javascript

Objects in Javascript are free form which means that we can add/remove fields and methods whenever we want. They are not bound to a particular class (like in Java, in fact there is no class concept in JS).

Easiest way to create a object is object inline like – “var obj = {};” Objects can also be created via object literal

 
// create object inline
var myObj = {}; 
// FREE FORM: dynamically attach property to the above object
myObj.prop1 = "Hello"; 
console.log(myObj);
// Prints the above attached property
console.log(myObj1.prop1); 
// If we try to access the property which is not declared in object, we get it as 'undefined'
console.log(myObj1.prop2); 
delete myObj1.prop1; // FREE FORM: delete property from an Object

Properties and Access Specifiers
All properties within object are public as they dont come with any access specifier. Properties of an object can be accessed in 2 ways
– Using dot notation
– Using square brackets
When to use dot notation vs square brackets?

  • Use [] notation
    • when property is a reserved keyword or invalid identifier
    • when property name is dynamic
    •  var myObj1 = {
      "prop1": "Hello",
      "prop2": 10,
      "1": "one"
      }
      console.log(myObj1.1); // will result in error
      var propertyName = "prop2";
      console.log(myObj1.propertyName);
      
  • Prefer DOT NOTATION over [] NOTATION because JS Engine can do some optimizations upfront with DOT NOTATION. And DOT NOTATION is also faster compared to [] approach.

Two objects can be compared with ‘===’ operator
var myObj2 = myObj1;
(myObj1 === myObj2) -> returns true

Objects with undefined and null
If we dont define a property then its gonna be undefined, but if we wanna define a property with empty value then we initialize it with null value.

Introduction to javascript

Javascript is created in early 90s by Brendan Eich at Netscape and is later introduced as a standard specification by ECMA committee. Current version supported by most browsers is ECMA5 and newly released version is ECMA 2015 aka ECMA6

Javascript is a lightweight, interpreted or JIT compiled programming language with first class functions.
It is a prototype based, multi-paradigm, dynamic language, scripting language, supporting object oriented, imperative, declarative and functional programming styles. There are lot of buzz words, and we will see each one of them

  • Lightweight: It has very less foot print in the machine that its running
  • Interpreted: We do not explicitly compile JS programs (like we do in JAVA, instead its compiled on the go)
  • First Class Functions: Functions are first class citizens in JS which means
    • we can assign functions to variables
    • we can pass functions as method params
    • we can return a function as a return type from a method
  • Multi Paradigm: It can support all programming paradigms
  • Object Oriented: model state and behavior around objects to do sth
  • Imperative: step by step instructions on HOW TO DO sth (like C)
  • Declarative: we tell WHAT TO DO rather than HOW TO DO (like scala)
  • Functional: subset of declarative language style (scala)
  • Dynamic Language: Method binding to a object is done at runtime rather than at the compile time and during compilation time, compiler wont report (for example, like Java does)
  • Scripting Language: Instructions written to execute on runtime environment (like unix scripting enables with the deployment of web applications), JS is used with modifying DOM structure at the browser runtime.

Why so many programmers are not comfortable with JS by tagging it as a front end technology?
– Its because of the above traits of the language.
– Because of backward compatibility lot of bugs remained as bugs forever in JS (like == vs === , null typeof object, so on)
– Initially when it was introduced, it was meant to be friendly language and the hence it internally did lot of type coercions

Why learn JS? With NodsJS, Javascript evolved a lot and now it is being used widely across different layers

  • Client side web development
    • Native JS
    • JQuery
    • Angular, React
  • Server side
    • NodeJS
    • Express
    • Browser Extensions
      so on..

Javascript runtime is usually a browser but as we learn this we can either use nodejs or mozilla firefox’s scratchpad to write and execute JS programs.

References:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference
https://www.youtube.com/user/koushks

Mawazo

Mostly technology with occasional sprinkling of other random thoughts

amintabar

Amir Amintabar's personal page

101 Books

Reading my way through Time Magazine's 100 Greatest Novels since 1923 (plus Ulysses)

Seek, Plunnge and more...

My words, my world...

ARRM Foundation

Do not wait for leaders; do it alone, person to person - Mother Teresa

Executive Management

An unexamined life is not worth living – Socrates

javaproffesionals

A topnotch WordPress.com site

thehandwritinganalyst

Just another WordPress.com site

coding algorithms

"An approximate answer to the right problem is worth a good deal more than an exact answer to an approximate problem." -- John Tukey

%d bloggers like this: