Introducci贸n a JavaScript
驴Por qu茅 aprender JavaScript?
隆Hola Mundo! en Mac
隆Hola Mundo! en Windows
Anatom铆a de una variable
Tipos de datos en JavaScript
Creaci贸n de strings
Operadores aritm茅ticos
Conversi贸n de tipos: Type Casting y Coerci贸n
Conversi贸n de tipos expl铆cita e impl铆cita
Estructuras de Control y L贸gica
Operadores de comparaci贸n
Operadores l贸gicos
Ejecuci贸n condicional: if
Ejercicio: Adivina el n煤mero
Ejecuci贸n condicional: switch
Loop: for
Loop: for of
Loop: for in
Loop: while
Loop: do while
Funciones y This
Anatom铆a de una funci贸n
Funciones vs M茅todos
Funciones puras e impuras
Arrow function y enlace l茅xico
Contextos de ejecuci贸n y scope chain
驴Qu茅 es Closure?
Preguntas a Desarrolladores Senior: 驴Por qu茅 aprender Desarrollo Web?
Manipulaci贸n de Arrays
Introducci贸n a Arrays
Mutabilidad e inmutabilidad de Arrays
Modificaci贸n b谩sica del final con push( ), pop( )
Iteraci贸n con map( ) y forEach( )
Filtrado y reducci贸n con filter( ) y reduce( )
B煤squeda de elementos con find( ) y findIndex( )
Crear copias con slice( )
Spread operator: casos de uso
Programaci贸n Orientada a Objetos
Anatom铆a de un Objeto
Trabajando con objetos en JavaScript
Funci贸n constructora
驴Qu茅 es una clase?
Prototipos y herencias
Herencia en la pr谩ctica
Prototipos en la pr谩ctica
this en JavaScript
Proyecto: Crea una red social
Proyecto: Crea una red social parte 2
Asincron铆a en JavaScript
驴C贸mo funciona el JavaScript Engine?
Promesas en JavaScript
Usando Async y await en JavaScript
For await of
驴C贸mo funciona la web?
驴C贸mo funciona HTTP?
M茅todo GET en JavaScript
M茅todo POST en JavaScript
M茅todo DELETE en JavaScript
Importancia del id en el m茅todo DELETE
ECMAScript 6 y tus siguientes pasos
You don't have access to this class
Keep learning! Join and start boosting your career
The use of the Switch
control structure is fundamental in programming to handle multiple scenarios based on a specific expression. Comparable to if, else if, else,
Switch
allows multiple validations, but with one essential difference: it evaluates only if an expression is true and not multiple conditions.
Unlike the comparative operators in if
structures, Switch
is used exclusively to validate whether the expression we are evaluating is true.
But how do we set up a Switch
and what essential components make up its structure?
The structure of a Switch
begins by defining an expression in parentheses. This expression is the condition to be evaluated. Then, by creating multiplecases
, we specify what should happen if the expression matches a given value. Let's see how to set it up:
let expression = 'papayas';
switch (expression) { case 'oranges': console.log('Oranges cost 20 pesos a kilo.'); break; case 'apples': console.log('Apples cost 43 pesos a kilo.'); break; case 'bananas': console.log('Bananas cost 30 pesos per kilo.'); break; case 'mangos': case 'papayas': console.log('Mangoes and papayas cost 25 pesos per kilo.'); break; default: console.log('Sorry, we don't have ' + expression);}
The break
statement in a Switch
is crucial because it prevents the execution of subsequent cases once a true
has been encountered. In case no condition is met, we can define a default
to handle unforeseen scenarios and provide predefined responses.
break
: Interrupts the execution of the Switch
so that, once a case has been validated, it does not continue to evaluate the following cases.default
: Used as the "else" of a Switch
, providing an output when no condition is true.Switch
is ideal when a single variable or expression must be evaluated against multiple literal values. Unlike if-else
, which is more flexible for complex comparisons (such as greater than or less than), Switch
works only with strict comparison (as if we use the triple equal ===
). Therefore, when choosing between the two, consider:
Switch
for conditions with many predefined branches.Switch
is more efficient for exact equality.if-else
for logical complexity or advanced comparisons.Through the use of Switch
, programmers can design more organized and efficient decision flows in situations where only equality of values needs to be checked. Experiment with it and see for yourself the agility it can offer to your codes!
Contributions 26
Questions 1
Want to see more contributions, questions and answers from the community?