Last active
March 10, 2016 21:00
-
-
Save pilinux/0614f3db39e36b6c2ec9 to your computer and use it in GitHub Desktop.
Javascript code snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.alert("Hello World!"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.write("Hello World!"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var game = "watchDog"; | |
message = (game=="watchDog") ? "I love UbiSoft! <br>" : "Fifa rocks! <br>"; | |
document.write(message); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x = 0; | |
do { | |
document.write("i = " + x + "<br>"); | |
x++; | |
} | |
while (x<5); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x = 10; | |
do { | |
document.write("i = " + x + "<br>"); | |
x++; | |
} | |
while (x<5); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i = 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do { | |
code to be executed | |
} | |
while (condition); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i = 0 | |
i = 1 | |
i = 2 | |
i = 3 | |
i = 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (i=0; i<5; i++) { | |
document.write("i = " + i + "<br>"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var obj = {number:10, bool:true, string:"www.masteringcodes.com"}; | |
for (var prop in obj) { | |
document.write(prop + ' - ' + obj[prop] + '<br />'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (variable in object) { | |
instructions | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
number - 10 | |
bool - true | |
string - www.masteringcodes.com |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (initialization; condition; increment) { | |
code to be executed | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i = 0 | |
i = 1 | |
i = 2 | |
i = 3 | |
i = 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = [10, 'www.masteringcodes.com', "https://www.masteringcodes.com"]; | |
// callback function | |
function myArr(val, index) { | |
document.write('['+ index + '] = ' + val +'<br/>'); | |
} | |
arr.forEach(myArr); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[0] = 10 | |
[1] = www.masteringcodes.com | |
[2] = https://www.masteringcodes.com |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function multiply(a, b) { | |
return a*b; | |
} | |
var x = multiply(5,15); | |
document.write(x); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form> | |
<input type="button" onclick="msg('It\'s pizza time!')" value="Pizza" /> | |
<input type="button" onclick="msg('Let\'s play fifa!')" value="Fifa" /> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function msg(text) { | |
alert(text); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function functionName(parameter1, parameter2, ...) { | |
code to be executed | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var va = 'variable'; | |
function myF() { | |
var va = 'function'; | |
document.write(va + '<br>'); | |
} | |
// Calling the function | |
myF(); | |
//Printing 'va' | |
document.write(va + '<br>'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var va = 'variable'; | |
function myF() { | |
va = 'function'; | |
document.write(va + '<br>'); | |
} | |
// Calling the function | |
myF(); | |
//Printing 'va' | |
document.write(va + '<br>'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form> | |
<input type = "button" value = "Factorial 4" onclick="alert(factorial(4))" > | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function factorial(n) { | |
var result; | |
if (n>0) result = n*factorial(n-1); | |
else if (n==0) result = 1; | |
else result = null; | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var game = "fifa"; | |
if (game=="fifa") | |
document.write("Fifa rocks! <br>"); | |
else | |
document.write("I love UbiSoft! <br>"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Learn JavaScript</title> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
document.write("Hello World!"); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Learn JavaScript</title> | |
</head> | |
<body> | |
<script type="text/javascript" src="code.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var i = 0; | |
while (i<5) { | |
document.write("i = " + i + "<br>"); | |
i++; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while (condition) { | |
code to be executed | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i = 0 | |
i = 1 | |
i = 2 | |
i = 3 | |
i = 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var question = window.confirm("The result of 10+0 is 10?"); | |
if | |
(question) alert("Correct answer!"); | |
else | |
alert("Wrong answer!"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.confirm("Question"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var msg = window.prompt("Write your name", "First name Last name"); | |
alert(msg); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.prompt("Message", DefaultValue); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment