Skip to content

Instantly share code, notes, and snippets.

@mjzone
Last active July 3, 2016 05:30
Show Gist options
  • Save mjzone/778047d65696de2fc1d558c909bccbc6 to your computer and use it in GitHub Desktop.
Save mjzone/778047d65696de2fc1d558c909bccbc6 to your computer and use it in GitHub Desktop.
Example 01
var myString = "Manoj";
var myNumber = 42;
myString = 32;
console.log('42' - 2); // Result: 40
console.log('42' + 2); // Result: 422 gotcha! (Javascript infers the type of variable at Runtime.)
// Javascript assumes you want to concat two strings.
var anotherString = '42'
console.log(parseInt(anotherString, 10) + 2); // Be explicit about your variable types.
// use typeof to determine primitive types.
console.log(typeof 'Manoj'); // string
console.log(typeof 32); // number
var person = {
name: 'Manoj Fernando',
age: '27'
}
console.log(typeof person); // object
var people = [{name: "Manoj"}, {name: "Kasun"}]
console.log(typeof people) // object. gotcha! (don't use typeof for instances. use instanceof)
console.log(people instanceof Array) // true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment