Skip to content

Instantly share code, notes, and snippets.

@pilinux
Last active March 10, 2016 21:00
Show Gist options
  • Save pilinux/0614f3db39e36b6c2ec9 to your computer and use it in GitHub Desktop.
Save pilinux/0614f3db39e36b6c2ec9 to your computer and use it in GitHub Desktop.
Javascript code snippets
window.alert("Hello World!");
document.write("Hello World!");
var a,b,c,d,e,f,g,h,i,j,k=21,l=25,m=35,n=36,o=37,p,q,r,s,t,u,v,w;
a = "I am javascript";
b = a;
c = 10;
d = 5;
e = 3;
f = c + d;
g = c - d;
h = c * e;
i = c / d;
j = c % e;
k += g;
l -= g;
m *= l;
n /= m;
o %= d;
p = c < d;
q = c > d;
r = c <= d;
s = c >= d;
t = c == d;
u = c != d;
v = !(c == d) && (s);
w = p || q;
document.write(
"Hello World!" + "<br>" +
b + "<br>" +
"f " + "= " + f + "<br>" +
"g " + "= " + g + "<br>" +
"h " + "= " + h + "<br>" +
"i " + "= " + i + "<br>" +
"j " + "= " + j + "<br>" +
"k " + "= " + k + "<br>" +
"l " + "= " + l + "<br>" +
"m " + "= " + m + "<br>" +
"n " + "= " + n + "<br>" +
"o " + "= " + o + "<br>" +
"p " + "= " + p + "<br>" +
"q " + "= " + q + "<br>" +
"r " + "= " + r + "<br>" +
"s " + "= " + s + "<br>" +
"t " + "= " + t + "<br>" +
"u " + "= " + u + "<br>" +
"v " + "= " + v + "<br>" +
"w " + "= " + w + "<br>" +
typeof parseFloat + "<br>" +
typeof a + "<br>" +
typeof x + "<br>" +
typeof c + "<br>" +
typeof v + "<br>" +
typeof window + "<br>" +
typeof document.write
);
var game = "watchDog";
message = (game=="watchDog") ? "I love UbiSoft! <br>" : "Fifa rocks! <br>";
document.write(message);
var x = 0;
do {
document.write("i = " + x + "<br>");
x++;
}
while (x<5);
var x = 10;
do {
document.write("i = " + x + "<br>");
x++;
}
while (x<5);
do {
code to be executed
}
while (condition);
i = 0
i = 1
i = 2
i = 3
i = 4
for (i=0; i<5; i++) {
document.write("i = " + i + "<br>");
}
var obj = {number:10, bool:true, string:"www.masteringcodes.com"};
for (var prop in obj) {
document.write(prop + ' - ' + obj[prop] + '<br />');
}
for (variable in object) {
instructions
}
number - 10
bool - true
string - www.masteringcodes.com
for (initialization; condition; increment) {
code to be executed
}
i = 0
i = 1
i = 2
i = 3
i = 4
var arr = [10, 'www.masteringcodes.com', "https://www.masteringcodes.com"];
// callback function
function myArr(val, index) {
document.write('['+ index + '] = ' + val +'<br/>');
}
arr.forEach(myArr);
[0] = 10
[1] = www.masteringcodes.com
[2] = https://www.masteringcodes.com
function multiply(a, b) {
return a*b;
}
var x = multiply(5,15);
document.write(x);
<form>
<input type="button" onclick="msg('It\'s pizza time!')" value="Pizza" />
<input type="button" onclick="msg('Let\'s play fifa!')" value="Fifa" />
</form>
function msg(text) {
alert(text);
}
function functionName(parameter1, parameter2, ...) {
code to be executed
}
var va = 'variable';
function myF() {
var va = 'function';
document.write(va + '<br>');
}
// Calling the function
myF();
//Printing 'va'
document.write(va + '<br>');
var va = 'variable';
function myF() {
va = 'function';
document.write(va + '<br>');
}
// Calling the function
myF();
//Printing 'va'
document.write(va + '<br>');
function addNumbers(a,b,c,d,e) {
if (!a) a = 0;
if (!b) b = 0;
if (!c) c = 0;
if (!d) d = 0;
if (!e) e = 0;
return a + b + c + d + e;
}
document.write("1 + 2 + 3 = " + addNumbers(1,2,3));
function addAndSquare(a,b) {
// Get the summation of 'a' and 'b'
var sum = a+b;
// Nested function
function multiply(x) {
return x*x;
}
return multiply(sum);
}
//first add 8 + 12 = 20, then sqrt(20) = 400
document.write( addAndSquare(8,12) );
<form>
<input type = "button" value = "Factorial 4" onclick="alert(factorial(4))" >
</form>
function factorial(n) {
var result;
if (n>0) result = n*factorial(n-1);
else if (n==0) result = 1;
else result = null;
return result;
}
function addNumbers() {
// Sets a variable that will return the final result
var sum = 0;
// go through the arguments Array and adds every value to "sum" variable
for(var i=0; i<addNumbers.arguments.length; i++) {
sum += addNumbers.arguments[i];
}
return sum;
}
document.write("1 + 2 + 3 = " + addNumbers(1,2,3));
var game = "fifa";
if (game=="fifa")
document.write("Fifa rocks! <br>");
else
document.write("I love UbiSoft! <br>");
<html>
<head>
<title>Learn JavaScript</title>
</head>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
<html>
<head>
<title>Learn JavaScript</title>
</head>
<body>
<script type="text/javascript" src="code.js"></script>
</body>
</html>
Hello World!
I am javascript
f = 15
g = 5
h = 30
i = 2
j = 1
k = 26
l = 20
m = 700
n = 0.05142857142857143
o = 2
p = false
q = true
r = false
s = true
t = false
u = true
v = true
w = true
function
string
undefined
number
boolean
object
function
var d = new Date();
var day = d.getDay();
switch(day) {
case 0:
document.write("Today is Sunday");
break;
case 1:
document.write("Today is Monday");
break;
case 2:
document.write("Today is Tuesday");
break;
case 3:
document.write("Today is Wednesday");
break;
case 4:
document.write("Today is Thursday");
break;
case 5:
document.write("Today is Friday");
break;
default:
document.write("Today is Saturday");
}
var i = 0;
while (i<5) {
document.write("i = " + i + "<br>");
i++;
}
while (condition) {
code to be executed
}
i = 0
i = 1
i = 2
i = 3
i = 4
var question = window.confirm("The result of 10+0 is 10?");
if
(question) alert("Correct answer!");
else
alert("Wrong answer!");
window.confirm("Question");
var msg = window.prompt("Write your name", "First name Last name");
alert(msg);
window.prompt("Message", DefaultValue);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment