//Show a variable uninitialized is set to undefined by the runtime
var x;
var y = 100;
console.log(x);
console.log(y);
//When you hit run, you will notice - undefined, 100 and another undefined. Ignore the last undefined as it is the //return value of invoking console.log(...) method. So always ignore the last undefined in a console.log(...) call
Typeof operator
var name = "John";
var cost = 99.25;
var salary;
var str = null;
function fn() { console.log("I am a function!"); }
console.log(typeof(name));
console.log(typeof(cost));
console.log(typeof(salary));
console.log(typeof(str));
console.log(typeof(fn));
Floating point approximation errors
0.1 + 0.2
Dividing by zero
100/0
-99/0
String to number conversion
parseInt("100");
parseInt("hello");
Mixing concatenation operator with numbers and strings could lead to unpredicatable results
'37' + 7;
//But...
'37' - 7;
Instantiating Objects
var obj1 = new Object();
var obj2 = {};
var user = { first_name : "John", last_name : "Doe"};
user.age = 25;
user
Arrays
var even = new Array();
even.push(2);
even.push(4);
even.push(6);
even;
var odd = [3, 5, 7];
odd;
Iterating over an array
var colors =["red", "blue", "green"];
for (var i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
class Shape {
constructor() {
this.X = 0;
this.Y = 0;
}
move(x, y) {
this.X = x;
this.Y = y;
}
distanceFromOrigin() {
return Math.sqrt(this.X * this.X + this.Y * this.Y);
}
}
const s = new Shape();
s.move(10, 10);
console.log("Distance from origin ", s.distanceFromOrigin());
//Interestingly if you look at the type of Shape it would not say class and instead function - so classes are behind //the scenes are still implemented as Constructor functions
typeof(Shape)