Today I learned how to track keypresses and update DOM with the values. I also learnt the power of using ternary operator to minimize writing long if...then...else statements.
As per MDN, the ternary operator is written like this: condition ? exprIfTrue : exprIfFalse
There are three parameters used in a ternary operator. Parameter 1 takes a condition that needs to be evaluated. Parameter 2 is executed if the condition is true. Parameter 3 is executed if the condition is false.
In this project, the ternary operator was used like this : `${event.key === ' ' ? 'Space' : event.key}`
The above expression states that if the user presses the spacebar, then pass the string value of 'Space' as event.key in case of spacebar is an empty value, else pass the value of the event.key
Category | HTML / JS / CSS |
Credits | 50 Projects in 50 days |
Date | January 24th, 2025 |
Codepen | Click here |