fbpx

JavaScript Snippets

window onLoad
Description:
Example:

get element
Description:gets an html element by its id tag
Example:

Display Information
Description:
Example:document.write('text');

Variable
Description:A variable stores a value in memory
Example:let animal="cat"; let score=10;

Concat Strings
Description:This will make two strings (text) into one string
Example:let fullName=firstName+" "+lastName;

If Statement
Description:runs code if a condition is true
Example:if (score>highscore) { highscore=score; }

if then else
Description:runs the first brackets of code if the condition is true and the second if false
Example:if (age>17) { document.write("adult"); } else { document.write("child"); }

functions
Description:groups together lines of code
Example:function addMe(num1,num2) { return num1+num2; }

Loop
Description:loops a variable through a range of numbers
Example: for (let i = 0; i < 11; i++) { document.write(i); }

Arrays
Description:a variable that has values by indexes, also can be considered a list
Example:let myArray=[10,22,35,41,53]; for(let i=0;i"); }