JavaScript

Math Object

Comparison and Logical Operators

if else and else if

Validation

.round(x)

Math.round(4.7); // returns 5

.pow()

Math.pow(8, 2); // returns 64

.sqrt ()

Math.sqrt(64); // returns 8

.abs()

Math.abs(-4.7); // returns 4.7

Comparison Operators

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 2 < 12;
</script>

Subtema

How Can it be Used

if (age < 18) text = "Too young";

Conditional (Ternary) Operator

variablename = (condition) ? value1:value2

Comparing Different Types

age = Number(age);
if (isNaN(age)) {
voteable = "Input is not a number";
} else {
voteable = (age < 18) ? "Too young" : "Old enough";
}

The if Statement

if (condition) {
// block of code to be executed if the condition is true
}

The else Statement

if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}

The else if Statement

if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}

puede validar la entrada numérica

<script>
function myFunction() {
var x, text;



// Get the value of the input field with id="numb"

x = document.getElementById("numb").value;



// If x is Not a Number or less than one or greater than 10

if (isNaN(x) || x < 1 || x > 10) {

text = "Input not valid";

} else {

text = "Input OK";

}

document.getElementById("demo").innerHTML = text;

}

Validación de datos

del servidor

del cliente