Best short notes on programming concepts & questions asked in interviews

Share sandboxes with short explanation with the community

Post your first short note
# All# javascript# hoisting# primitives# arrow-functions# javascriptintro# usefullinks

Latest from community today

What javascript cannot do:

- Cannot read files on the hardisk

- Things like, camera, microphone, location can be accessed via javascript but only by taking permission from the user

- Windows from two different domains cannot know about each other. This is called the "Same Origin Policy"

- Cross origin requests from one domain to another are not allowed unless the other domain allows it explicitly or a separate routing is setup.
/**
There’s another very simple and concise syntax for creating functions, that’s often better than Function Expressions.

It’s called “arrow functions”, because it looks like this:
*/
let func = (arg1, arg2, ...argN) => expression


// Here is non-arrow version of the above function
let func = function(arg1, arg2, ...argN) {
  return expression;
};



let sum = (a, b) => a + b;

/* This arrow function is a shorter form of:

let sum = function(a, b) {
  return a + b;
};
*/
console.log( sum(1, 2) ); // 3




let double = n => n * 2;
// roughly the same as: let double = function(n) { return n * 2 }

console.log( double(3) ); // 6



let sayHi = () => console.log("Hello!");

sayHi();






let sum = (a, b) => {  // the curly brace opens a multiline function
  let result = a + b;
  return result; // if we use curly braces, then we need an explicit "return"
};

console.log( sum(1, 2) ); // 3
console.log( "6" / "2" ); // 3, strings are converted to numbers




let str = "123";
console.log(typeof str); // string

let num = Number(str); // becomes a number 123

console.log(typeof num); // number




let age = Number("an arbitrary string instead of a number");

console.log(age); // NaN, conversion failed



console.log( Number("   123   ") ); // 123
console.log( Number("123z") );      // NaN (error reading a number at "z")
console.log( Number(true) );        // 1
console.log( Number(false) );       // 0




console.log( Boolean(1) ); // true
console.log( Boolean(0) ); // false

console.log( Boolean("hello") ); // true
console.log( Boolean("") ); // false



console.log( Boolean("0") ); // true
console.log( Boolean(" ") ); 


/**

HOISTING

*/
var x = "global scope";
function foo() {
  var x = "functional scope";
  console.log(x);
}
foo(); // "functional scope"
console.log(x); // "global scope"


// What if we were to drop the second var
var y = "global scope";
function bar() {
  y = "functional scope";
  console.log(y);
}
bar(); // "functional scope"
console.log(y); // "functional scope"


/**

SCOPE

*/
var a = 0; 
let b = 0;
const c = 0;
if (true) {
  var a = 1;
  let b = 1; 
  const c = 1;
}
console.log(a); // 1
console.log(b); // 0
console.log(c); // 0
// Similarities

console.log(!!null); //logs false
console.log(!!undefined); //logs false

console.log(Boolean(null)); //logs false
console.log(Boolean(undefined)); //logs false


// Differences 
// Undefined
let _thisIsUndefined;
  const doNothing = () => {};
  const someObj = {
    a : "ay",
    b : "bee",
    c : "si"
  };

  console.log(_thisIsUndefined); //logs undefined
  console.log(doNothing()); //logs undefined
  console.log(someObj["d"]); //logs undefined

//null
fs.readFile('path/to/file', (e,data) => {
  console.log(e); //it logs null when no error occurred
  if(e){
    console.log(e);
  }
  console.log(data);
});


// Also
console.log(null == undefined); // logs true
console.log(null === undefined); // logs false


Featured content