Javascript sintaxis Flashcards
cheat sheet javascript
Function: Delay - 1 second timeout
setTimeout(function () { }, 1000);
Functions, example: add numbers (1 + 2)
function addNumbers(a, b)
{ return a + b; }
x = addNumbers(1, 2);
Edit DOM element by ID
document.getElementById(“elementID”).innerHTML
Output console
console.log(a); // write to the browser console
Output prompt
prompt( “Your age?”, “0” );
// input dialog.
// Second argument is the initial value
Output write to the html
document.write(a);
// write to the HTML
Output confirm
confirm(“Really?”);
// yes/no dialog
//returns true/false depending on user click
Output alert
alert(a); // output in an alert box
Comments
/* Multi line comment */
// One line
Loop for
i: i*3, from 0 to 9
example:
‘0: 0’
‘1: 3’
‘2: 6’
‘3: 9’
…
for (var i = 0; i < 10; i++)
{
document.write
(i + “: “ + i*3 + “<br></br>”);
}
Loop for parsing an array
Toma un arreglo arr y calcula la suma de todos sus elementos.
var sum = 0;
for (var i = 0; i < arr.length; i++)
{ sum + = arr[i]; }
// parsing an array
Loop for of
Crea una cadena de texto con el código HTML de una lista no ordenada, donde cada elemento de la lista corresponde a un elemento del array custOrder
html = “ “;
for (var i of custOrder) {
html += “<li>” + i + “</li>”;
}
/El bucle for…of solo lee los elementos del array, no los modifica/
While Loop
Mientras i < 100
i *= 2;
Output:
//’2, ‘ ‘4, ‘ ‘8, ‘ ‘16, ‘ ‘32, ‘ ‘64, ‘ ‘128, ‘ */
var i = 1; // initialize
while (i < 100) {
/*enters the cycle if statement is true */
i * = 2;
/ * increment to avoid infinite loop //’2, ‘ ‘4, ‘ ‘8, ‘ ‘16, ‘ ‘32, ‘ ‘64, ‘ ‘128, ‘ * /
document.write(i + “, “);
// output 128 }
Do While Loop
Calcula y muestra las potencias de 2 hasta que se alcanza o supera el número 100.
var i = 1; // initialize
do { // enters cycle at least once
i * = 2; // increment to avoid
infinite loop
console.log(i + “, “); // output
} while (i < 100)
// repeats cycle if statement is true at the end
console.log(i);
Break
Imprime los números del 0 al 10, el bucle se detiene cuando i es igual a 5
Por lo tanto imprime hasta el 4
for (var i = 0; i < 10; i++) {
if (i == 5) {
break;
} // stops and exits the cycle
document.write(i + “, “);
// last output number is 4 }
Continue.
Este código itera de 0 a 9 e imprime cada número, excepto el 5, separándolos con una coma.
for (var i = 0; i < 10; i++) {
if (i == 5) {
continue;
} // skips the rest of the cycle
document.write(i + “, “);
// skips 5
}
Objects
Example: student
Incluye la declaracion y el uso
El objeto tiene una función por dentro
var student = { // object name
// list of properties and values
firstName:”Jane”,
lastName:”Doe”,
age:18,
height:170,
fullName : function( ) {
// object function
return this.firstName + “ “ + this.lastName;
}
};
student.age = 19;
// setting value
student[age]++;
// incrementing
name = student.fullName();
// call object function
DOM Node Properties
attributes
Gets a live list of all the characteristics associated with an element.
DOM Node Properties
baseURI
Returns an HTML element’s absolute base URL.