<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
if (273 < 52){
alert('273 < 52')
} if (273 > 52){
alert('273 > 52')
}
</script>
</head>
<body>
</body>
</html>
### Document Structure and Metadata
- The document begins with the `<!DOCTYPE html>` declaration, indicating that this is an HTML5 document.
- The `<html lang="en">` tag specifies that the document's primary language is English.
- Within the `<head>` section, there's a `<meta charset="UTF-8">` tag, setting the character encoding to UTF-8, which supports most characters from all languages, ensuring the document's text is displayed correctly.
- The `<title>` tag sets the browser's title/tab text to "Title".
### JavaScript Conditional Statements
- Inside the `<head>`, a `<script>` tag contains JavaScript code that executes when the page is loaded.
- The JavaScript code uses an `if` statement to compare two values, `273` and `52`:
- The first condition checks if `273` is less than `52`, which is false. Therefore, the corresponding `alert('273 < 52')` is not executed.
- The second condition checks if `273` is greater than `52`, which is true. Since there's no `else` or `else if` linking this condition to the first, it is evaluated independently and found to be true. As a result, `alert('273 > 52')` is executed, displaying an alert box with the message "273 > 52".
- Note that there's a missing `else` or `else if` statement that would typically be used to link these two conditions logically. However, the script as written evaluates each `if` statement independently.
### Output
- When the HTML document is loaded in a web browser, the script immediately checks the two conditions.
- Given the conditions, the browser displays an alert box with the message "273 > 52" because the second condition evaluates to true.
### Best Practices
- This example is simple and serves to demonstrate basic conditional logic in JavaScript. In practice, related conditions are often linked with `else` or `else if` to form more comprehensive conditional logic structures.
- Using `alert()` is helpful for quick demonstrations or debugging, but it's not typically used in modern web applications for user interactions due to its intrusive nature. For user interactions, more user-friendly approaches are generally preferred, such as updating the DOM or using modals.
'개념 > 혼자 공부하는 Javascript' 카테고리의 다른 글
chapter 3) Using if conditional statements (0) | 2024.11.25 |
---|---|
chapter 2) Example of using increment/decrement operators (0) | 2024.11.24 |
chapter 2) Using compound assignment operators (0) | 2024.11.22 |
chapter 1) basic Javascript (0) | 2024.11.21 |
chapter 5) Array methods and arrow functions (0) | 2024.04.03 |