JavaScript Objects
JavaScript Objects
by dshilkret
HTML
Do you know JavaScript? Are you sure? Let's test with some basics then. Are you ready?
JavaScript
console.log(2 + 2);
console.log(2 + "2");
console.log(2 + "2" + 2);
console.log(2 + 2 + "2" + 2);
console.log(2 + 2 + "2" + 2 + 2);
console.log(2 + "2" + 2 + "2" + 2 + 2);
console.log("2" + 2 + "2" + 2 + 2 + 2 + 2);
console.log("This is because of the dynamica nature of javascript")
//console.log(a);
var b;
console.log(b);
console.log("Now we are going to find the type of undefined")
console.log(typeof(b));
console.log(typeof(undefined));
console.log(isNaN(b));
console.log(typeof(typeof(b)));
// Here the typeof will always returns string
console.log(typeof(String(b)));
console.log(typeof({a:b}));
console.log(typeof(null));
// Now let us see an example?
var a=1;
var b=a;
a=2;
console.log(b);
console.log(a);
// this is why the variable we just created are primitive types. In primitive data types the values are saved directly on the variable. If you are coming from the backgound of C#, you can understand the value typed variable.
//Now we have one more data type which is referenced variable where values are stored as a reference not a direct values. An example for a referenced type variable is Object.
// Now we will take an overview on JavaScript Objects, Do you know how to create an object.
var myName = {firstName: "Sibeesh", lastName: "Venu"};
//Now this is an object.
console.log(myName);
console.log(JSON.stringify(myName));
//an object is a collection of key-value pairs, like we see in the above object, firstName is key and "Sibeesh" is value. How can take any values from an object?
console.log(myName.firstName);
console.log(myName.lastName);
// Now we have given string value to our object as a key? Is there anyway we can give a key in number format? Let's have a look.
var myName = {1: "Sibeesh", 2: "Venu"};
// Now how can we access values of the keys 1 & 2? Like below?
//console.log(myName.1);
//console.log(myName.2);
//This will throw an "Uncaught SyntaxError" error, so how can we access...