<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
'use strict'
//error -> data = 10
const data = 10
console.log(data)
</script>
</head>
<body>
</body>
</html>
### Strict Mode Activation
- `'use strict'` at the beginning of the `<script>` tag activates strict mode for the script. In strict mode, JavaScript will throw more exceptions and prevent or throw errors for actions that are considered to be bad practices or common mistakes. This includes, but is not limited to, preventing the use of undeclared variables.
### Commented Out Error
- `//error -> data = 10` is a comment indicating that if you try to assign a value to an undeclared variable named `data`, it would result in an error in strict mode. This line is commented out to prevent the error from actually occurring and halting script execution.
### Correct Variable Declaration and Use
- `const data = 10` properly declares a constant named `data` with a value of `10`. Because `data` is declared, this line does not cause any issues, even in strict mode.
- `console.log(data)` logs the value of `data` to the console, which will be `10`.
### Purpose and Behavior
The purpose of this script is to illustrate that in strict mode, JavaScript requires variables to be declared before they are used. Attempting to use an undeclared variable (`data = 10`) will result in an error. The script also shows the correct way to declare and use a variable within strict mode (`const data = 10`).
When the document is loaded in a web browser, the console will output `10`, demonstrating that `data` was successfully declared and logged. If the commented-out line were uncommented (and `const data = 10` removed or commented out), the script would throw a ReferenceError, indicating that `data` is not defined, showcasing strict mode's enforcement against using undeclared variables.
'개념 > 혼자 공부하는 Javascript' 카테고리의 다른 글
chapter 5) fix name conflict issue (0) | 2024.12.08 |
---|---|
chapter 5) function_type (0) | 2024.12.07 |
chapter 5) Troubleshooting Using Immediately Called Functions (0) | 2024.12.05 |
chapter 5) use to remaining parameters make array (0) | 2024.12.04 |
chapter 5) arguments (0) | 2024.12.03 |