Java Script
Java Script
Java Script
JavaScript
<!doctype html>
<html>
<head>
<title>Code inside an HTML document</title>
<script>
alert("Hello, world!");
</script>
</head>
<body>
</body>
</html>
JavaScript
<!doctype html>
<html>
<head>
<title>Code inside an HTML document</title>
<script src="hello.js"></script>
</head>
<body>
</body>
</html>
JavaScript - DOM
El navegador convierte las etiquetas HTML, en una representación interna
llamada DOM Modelo de Objetos de Documento.
función onload()
Case Sensitive
Tipos:
1. number
2. string
3. Boolean
4. null
5. undefined
6. object
Null
Se usa cuando se define una variable y se define que no va tener ningún valor
Object
Colección de propiedades. Las propiedades pueden ser de tipo cualquiera de los
vistos anteriormente.
JavaScript - Operaciones
Concatenar
var fname, lname, fullName;
fname = "John";
lname = "Doe";
fullName = fname + " " + lname; // fullName is "John Doe"
Operaciones matemáticas ( +, -, *, / )
var widgets, gizmos, inventory;
widgets = 1043;
gizmos = 2279;
inventory = widgets + gizmos; // inventory is 3322
JavaScript - Operaciones
Operaciones matemáticas ( +, -, *, /, % )
var provincial, federal, subtotal, total;
provincial = 0.095;
federal = 0.05;
subtotal = 10;
total = subtotal + (subtotal * provincial) + (subtotal * federal); // total is 11.45
Importante
var johnTaskCount = 11,
janeTaskCount = "42",
totalTaskCount = johnTaskCount + janeTaskCount;
JavaScript - Comparaciones
Equal (==)
1 == 1 // returns true
"1" == 1 // returns true ("1" converts to 1)
1 == true // returns true
0 == false // returns true
"" == 0 // returns true ("" converts to 0)
" " == 0 // returns true (" " converts to 0)
0 == 1 // returns false
1 == false // returns false
0 == true // returns false
var x, y; // declare x and y
x = {}; // create an object and assign it to x
y = x; // point y to x
x == y; // returns true (refers to same object in memory)
x == {}; // returns false (not the same object)
JavaScript - Comparaciones
Not Equal (!=)
1 != 1 // returns false
"1" != 1 // returns false ("1" converts to 1)
1 != true // returns false
0 != false // returns false
"" != 0 // returns false ("" converts to 0)
" " != 0 // returns false (" " converts to 0)
0 != 1 // returns true
1 != false // returns true
0 != true // returns true
var x, y; // declare x and y
x = {}; // create an object and assign it to x
y = x; // point y to x
x != y; // returns false (refers to same object in memory)
x != {}; // returns true (not the same object)
JavaScript - Comparaciones
Strict Equal (===)
myArray.push("hello");
JavaScript - Array - Read Data
var myValue,
myArray = ["Hello", "World", "I", "am", "an", "array"];
var tasks = [
"Pay phone bill",
"Write best-selling novel",
"Walk the dog"
];
tasks.pop(); // returns "Walk the dog"
JavaScript - Array - Métodos
push()
Agrega un nuevo elemento al final del array y retorna la nueva longitud
var tasks = [
"Pay phone bill",
"Write best-selling novel",
"Walk the dog"
];
tasks.push("Feed the cat"); // returns 4
// tasks is now:
// ["Pay phone bill",
// "Write best-selling novel",
// "Walk the dog",
// "Feed the cat"]
JavaScript - Array - Métodos
reverse()
Invierte el orden de los elementos en el array
var tasks = [
"Pay phone bill",
"Write best-selling novel",
"Walk the dog"
];
tasks.reverse();
// tasks is now:
// ["Walk the dog",
// "Write best-selling novel",
// "Pay phone bill"]
JavaScript - Array - Métodos
shift()
Elimina el primer elemento del array y lo retorna
var tasks = [
"Pay phone bill",
"Write best-selling novel",
"Walk the dog"
];
tasks.shift(); // returns "Pay phone bill"
// tasks is now:
// ["Write best-selling novel",
// "Walk the dog"]
JavaScript - Array - Métodos
sort()
Ordena los elementos en orden ascendente. Todo se convierte en String y luego
se compara. Si el array es [3, 10, 1, 2], lo ordena [1, 10, 2, 3].
var tasks = ["Pay phone bill", "Write best-selling novel", "Walk the dog"];
tasks.sort(); // sorts array in ascending order
// tasks is now:
// ["Pay phone bill", "Walk the dog", "Write best-selling novel"]
array.sort([compare]);
JavaScript - Array - Métodos
splice()
Permite agregar y eliminar simultáneamente elementos a un array. Retorna los
elementos que elimina.
var tasks = [
"Pay phone bill",
"Write best-selling novel",
"Walk the dog"
];
tasks.splice(1, 1, "World domination");
// tasks is now:
// ["Pay phone bill",
// "World domination",
// "Walk the dog"]
JavaScript - Array - Métodos
splice()
var tasks = [
"Pay phone bill",
"Write best-selling novel",
"Walk the dog"
];
tasks.splice(1, 1, "World domination", "Rotate tires",
➥"Hire hit squad");
// tasks is now:
// ["Pay phone bill",
// "World domination",
// "Rotate tires",
// "Hire hit squad",
// "Walk the dog"]
JavaScript - Array - Métodos
unshift()
Agregar uno o más elementos al inicio del array, y retorna la nueva longitud.
var nums;
nums = [4, 8, 15, 16, 23, 42];
alert("The winning lottery numbers are: " + nums.join(", "));
JavaScript - Array - Métodos de acceso
slice()
Copia o extrae una parte de una array. Se indica el inicio y opcionalmente el final a
extraer.
var alphabet;
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
//Retorna 12
JavaScript - Array - Iteration Methods
forEach()
arr.forEach(function(num) {
total = total + num;
});
var myObject = {}; // Ésta forma es más preferible, sencillo y más seguro
profile = {
firstName: "Hugo",
lastName: "Reyes",
flight: "Oceanic 815",
car: "Camaro"
};
JavaScript - Objects
var obj = {};
obj["firstName"] = "Hugo";
obj["lastName"] = "Reyes";
La selección de la notación puede depender de algunas necesidades. Por ejemplo, los [] pueden contener
variables o espacios.