Location:
<script> *Your js here* </script>
<script src="script.js"></script>
Debugging:
Variables
var surname = prompt('Greetings friend, may I enquire as to your surname?'); //surname is now available for use!
<script>var addMe = function(a,b){ return (a + b); } //initialized to hold function definition </script>
<script>var result = addMe(5,6); //function call </script>
Operators
Conditional Statements
if(10 > 5){alert('10 is greater than 5');} else {alert('JS doesn't know math');}
Loopy statments!
while(10 > 5){alert('10 is greater than 5');}
for(var i=0; i < 10;i++){alert('10 is greater than 5');}
Arrays
var emptyArray = []; //array-literal syntax for creation var bestFootballClubsInOrder = ['Chelsea','Bayern Munich', 'Real Madrid', 'Barcelona']; bestFootballClubInOrder[0]; //=>?
Objects
var jedi = { name: "Yoda",//property that holds a string age: 899, talk: function () { alert("another... Sky... walk...");//property that holds function } };
Getting stuff from the DOM - using the 'document' object
var firstNameElement = document.getElementById('fName'); var firstName = firstNameElement.value; //Getting the value of the element. Better way?
Putting stuff into the DOM
var newDiv = document.createElement("div"); var newContent = document.createTextNode("Hi there and greetings!"); newDiv.appendChild(newContent); //add the text node to the newly created div. // add the newly created element and its content into the DOM var currentDiv = document.getElementById("div1"); document.body.insertBefore(newDiv, currentDiv);