Está en la página 1de 10

CREACION DE UNA VARIABLE EN JAVA SCRIPT

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>variables</title>
</head>
<body>
    <script>
    
    var nombre;
    nombre = prompt("ingresa tu nombre");

    alert(nombre);
    document.write(nombre);
    console.log(nombre);

CON CUALQUIERA DE ESTOS TRES PUEDES IMPRIMIR EN LA PAGINA TU NOMBRE, EL CONSOLE.LOG


SOLO APARECERA CUANDO ABRAS LA CONSOLA DE CHROME.
    </script>

    
</body>
</html>

TAMBIEN PODEMOS CONCATENAR VARIABLES. POR EJEMPLO

    <script>
    
    var nombre;
    nombre = prompt("ingresa tu nombre");
    edad = prompt("ingresa tu edad");

    document.write(nombre + " " + edad);
    </script>
    
</body>
</html>
TAMBIEN PODEMOS HACER OPERACIONES DENTRO DE LAS VARIABLES. EJEMPLO.

<script>
    
    var nombre;
    nombre = "Fabian";
    edad = 19 + 2;

    document.write(nombre + " " + edad);
    </script>

CREACION DE UN ARREGLO.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>variables</title>
</head>
<body>
    <script>
        var amigos = ["fabian", "geanfrancko", "ruiz"];
  
        document.write(amigos[0]);
Imprimirá “Fabian”, ya que es la posición cero.

    </script>
</body>
</html>
<body>
    <script>
        var amigos = ["fabian", "geanfrancko", "ruiz"];

si queremos reemplazar un nombre.

        amigos[0] = "fabigian"
        document.write(amigos[0]);

    </script>
</body>
</html>

SI DESEO SABER CUANTOS elementos TENGO EN EL ARREGLO


   <script>
        var amigos = ["fabian", "geanfrancko", "ruiz"];
        
        document.write( amigos.length );

CONCATENANDO
  <script>
        var amigos = ["fabian", "geanfrancko", "ruiz"];
        
        document.write( "Tienes" + amigos.length + "amigos" );

SEPARANDO LA CONCATENACION.

    <script>
        var amigos = ["fabian", "geanfrancko", "ruiz"];
        
        document.write( "Tienes" + " " + amigos.length + " " + "amigos" );

TAMBIEN SE PUEDE SEPARAR LA CONCATENACION DE LA SIGUIENTE MANERA.

    <script>
        var amigos = ["fabian", "geanfrancko", "ruiz"];
        
        document.write( "Tienes " +  amigos.length + " amigos" );
PUSH, en consola del navegador. Me agrega elementos al final del arreglo.

<script>
        var amigos = ["fabian", "geanfrancko", "ruiz", "fabigian"];
        
        amigos.push("carlos", "fransisco", "javier");

POP, elimina el último elemento.

<script>
        var amigos = ["fabian", "geanfrancko", "ruiz", "fabigian"];
        
        amigos.pop();

CONCATENANDO otro arreglo

   <script>
        var amigos = ["fabian", "geanfrancko", "ruiz", "fabigian"];
        var amigos2 = ["pedro", "fabio", "camila"]

        var amigos3 = amigos.concat(amigos2);
        document.write(amigos3);

JOIN, me permite separar por guiones, comas, dos puntos, etc. Los elementos.

    <script>
        var amigos = ["fabian", "geanfrancko", "ruiz", "fabigian"];
        
        document.write(amigos.join("-"))

SORT, ordena de forma alfabética.

<script>
        var amigos = ["fabian", "geanfrancko", "ruiz", "fabigian"];
        
        var ordenados = amigos.sort();
        document.write(ordenados);
CONDICIONALES

   <script>
        var nombre = "fabian",
            edad = 27

        if( nombre == "carlos"){
            document.write("bienvenido" + nombre);
        } else{
            document.write("bienvenido anonimo");
        }

Otro ejemplo de condicional, trabajando con operadores.

  <script>
        var nombre = "fabian",
            edad = 15

        if (edad > 18) {
            document.write("bienvenido, eres mayor de edad");
        } else {
            document.write("no eres mayor de edad");
        }

Otro ejemplo.

    <script>
        var nombre = "fabian",
            edad = 18

        if (edad >= 18) {
            document.write("bienvenido, eres mayor de edad");
        } else {
            document.write("no eres mayor de edad");
        }
Imprime, eres mayor de edad.
CICLOS

Te permite Imprimir una sola.

<script>
    
        for( i = 0; i <= 10; i++){
            document.write("ou yeah!" + "<br>");
        }

Otro ejemplo de ciclo: for

    <script>
    
        var numeroDeUsuarios = 70
        for( i = 1; i <= numeroDeUsuarios; i++){
            document.write(i + "<br>");
        }
        

Otro ejemplo del ciclo for

<script>
    
        var dias = ["lunes", "martes", "miercoles", "jueves", "viernes", "sabado", 
"domingo"];

        for( i = 0; i <= dias.length -1; i++){
            document.write( dias[i] + "<br>");
        }
FUNCIONES

    <script>
        function saludo(){
            document.write("hola soy fabian, en que puedo ayudarte");

        }
        
        saludo();

Otro ejemplo.

  <script>
        function saludo(nombre){
            document.write("hola " + nombre);

        }
        
        saludo("Arturo");

Otro ejemplo de Funcion

    <script>
        var suma = function (numero1, numero2) {
            var numero1 = numero1;
            var numero2 = numero2;

            return numero1 + numero2;
        }

        document.write (suma (20,39));

    </script>
Otro ejemplo claro,

   <script>
        var numeroMaximo = function(valor1, valor2){
            if ( valor1 > valor2 ){
                return valor1;
            }
            return valor2;
        }

        document.write("el numero maximo es: " + numeroMaximo(23,34));

    </script>
CALCULADORA

    <title>variables</title>
</head>
<body>

    <form action="">
        <input type="text" id="numero1">
        <input type="text" id="numero2">
        <input type="button" value="Sumar" onclick="alert( suma(10,50) );">

    </form>

    <script>
       
        var suma = function(numero1, numero2){
            var numero1 = parseInt(document.getElementById("numero1").value);
            var numero2 = parseInt(document.getElementById("numero2").value);

            var resultado = numero1 + numero2;
            return resultado;
        }

    </script>
</body>
</html>
CALCULADORA MULTIPLICAR

 <title>variables</title>
</head>
<body>

    <form action="">
        <input type="text" id="numero1">
        <input type="text" id="numero2">
        <input type="button" value="Multiplicar" onclick="alert( multiplicacion (10
,50) );">

    </form>

    <script>
       
        var multiplicacion = function(numero1, numero2){
            var numero1 = parseInt(document.getElementById("numero1").value);
            var numero2 = parseInt(document.getElementById("numero2").value);

            var resultado = numero1 * numero2;
            return resultado;
        }

    </script>
</body>
</html>
Si el resultada con decimales, cambar el parseInt por parseFloat.

También podría gustarte