Skip to content

Instantly share code, notes, and snippets.

@tedhagos
Created August 10, 2012 14:53
Show Gist options
  • Save tedhagos/3314767 to your computer and use it in GitHub Desktop.
Save tedhagos/3314767 to your computer and use it in GitHub Desktop.
JavaScript Core
var util = require('util')
var i = 1;
var j = 1;
for (i = 1 ; i < 10; i++) {
for (j =1 ; j < 10; j++) {
util.print(i * j + "\t");
}
util.print("\n");
}
/*
RESULT
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
*/
var s = "hello";
var ret;
switch (s) {
case "he":
ret = "he";
break;
case 1:
ret = 1;
break;
case true:
ret = "true";
break;
case "Hello".toLowerCase():
ret = "Hello";
break;
default:
ret = "nothing";
}
console.log(ret);
/*
RETURNS "Hello"
*/
/*
A small test on how JS treats types
*/
var s = "hello";
var int = 1;
var float = 1.0;
var boolean = true;
var vnull = null;
var vundefined = undefined;
var obj = new Object();
var arr = new Array();
function x() {
return 1;
}
function y() {
return
}
function z() {
}
// Let's see the typeof results
console.log("s " + typeof(s));
console.log("int " + typeof(int));
console.log("float " + typeof(float));
console.log("boolean " + typeof(boolean));
console.log("vnull " + typeof(vnull));
console.log("vundefined " + typeof(vundefined));
console.log("obj " + typeof(obj));
console.log("arr " + typeof(arr));
console.log("function x " + typeof(x()));
console.log("function y " + typeof(y()));
console.log("function z " + typeof(z()));
console.log("wildcard " + typeof(wildcard));
/*
RETURNS:
s string
int number
float number
boolean boolean
vnull object
vundefined undefined
obj object
arr object
function x number
function y undefined
function z undefined
wildcard undefined
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment