Last active
July 1, 2017 03:30
-
-
Save johnsi15/fc1f9269c2fcaa6620b0c2932b16a432 to your computer and use it in GitHub Desktop.
Formas de crear funciones en JavaScript
This file contains 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
let base = 5; | |
let height = 10; | |
// Escapando concatenacion con comillas nueva formas de concatenar contenido | |
// console.log(`Hola mundo ${base * height / 2}`); | |
// Funcion normal Forma #1 | |
// function areaTriangulo(base, height){ | |
// return base * height / 2; | |
// } | |
// Funcion guardada en una variable Forma #2 | |
// const areaTriangulo = function(base, height){ | |
// return base * height / 2; | |
// } | |
// Funcion con arroba funcion Forma #3 | |
// const areaTriangulo = (base, height) => { | |
// de esta forma se hace cuanto es mucho contenido con las llaves | |
// return base * height / 2; | |
// } | |
// Funcion con arroba funcion Forma #4 | |
const areaTriangulo = (base, height) => base * height / 2; | |
console.log(`Hola mundo -> ${areaTriangulo(base, height)}`); |
This file contains 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 varTest() { | |
var x = 31; | |
if (true) { | |
var x = 71; // misma variable! | |
console.log(x); // 71 | |
} | |
console.log(x); // 71 | |
} | |
function letTest() { | |
let x = 31; | |
if (true) { | |
let x = 71; // variable diferente | |
console.log(x); // 71 | |
} | |
console.log(x); // 31 | |
} | |
// Segundo example | |
var a = 5; | |
var b = 10; | |
if (a === 5) { | |
let a = 4; // El alcance es dentro del bloque if | |
var b = 1; // El alcance es dentro de la función | |
console.log(a); // 4 | |
console.log(b); // 1 | |
} | |
console.log(a); // 5 | |
console.log(b); // 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A las variables const no se le pueden reasignar valores
const edad = 24
edad = 40 // No se puede
edad++ // No se puede