--- id: javascript-strict-mode title: "JavaScript Strict Mode" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["use strict", "strict mode", "ECMAScript 5 strict", "strict directive", "JS strict"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "strict-mode", "use-strict", "es5"] raw_sources: ["https://www.w3schools.com/js/js_strict.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Strict Mode]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) `"use strict";` (ECMAScript 5) makes JavaScript run in strict mode, turning previously-tolerated "bad syntax" into real errors. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`"use strict"` is a directive** β€” it is a literal expression, ignored by older JavaScript versions, that tells the engine to execute code in strict mode. [S1] - **Introduced in ES5** β€” strict mode was added in ECMAScript 5. [S1] - **Two scopes** β€” declared at the top of a script it applies globally; declared at the top of a function it applies only inside that function. [S1] - **Must appear at the beginning** β€” the directive must be at the start of the script or function to be recognized. [S1] - **Turns mistakes into errors** β€” strict mode makes it easier to write secure code by converting silent bad syntax into thrown errors (e.g. prevents accidental globals). [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Opt into safety per file or per function** β€” place `"use strict";` at the top of a script (global) or a function (local). [S1] - **Declare before use** β€” strict mode forbids using undeclared variables/objects, forcing explicit declarations. [S1] - **Avoid deprecated/unsafe constructs** β€” `with`, octal literals, duplicate parameters, deleting variables, etc. are all disallowed. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Declaring strict mode** Strict mode is declared by adding `"use strict";` to the beginning of a script or a function. Declared at the beginning of a script, it has global scope (all code in the script executes in strict mode): [S1] ```javascript "use strict"; x = 3.14; // This will cause an error because x is not declared ``` Declared inside a function, it has local scope (only the code inside the function is in strict mode): [S1] ```javascript "use strict"; myFunction(); function myFunction() { y = 3.14; // This will cause an error } ``` **Why strict mode?** Strict mode makes it easier to write "secure" JavaScript. It changes previously accepted "bad syntax" into real errors. For example, in normal JavaScript, mistyping a variable name creates a new global variable; in strict mode this throws an error, so you cannot accidentally create a global variable. [S1] **Not allowed in strict mode** Using a variable (or object) without declaring it: [S1] ```javascript "use strict"; x = 3.14; // This will cause an error ``` ```javascript "use strict"; x = {p1:10, p2:20}; // This will cause an error ``` Deleting a variable (or object) or a function is not allowed: [S1] ```javascript "use strict"; let x = 3.14; delete x; // This will cause an error ``` ```javascript "use strict"; function x(p1, p2) {}; delete x; // This will cause an error ``` Duplicating a parameter name is not allowed: [S1] ```javascript "use strict"; function x(p1, p1) {}; // This will cause an error ``` Octal numeric literals and octal escape characters are not allowed: [S1] ```javascript "use strict"; let x = 010; // This will cause an error ``` ```javascript "use strict"; let x = "\010"; // This will cause an error ``` Writing to a read-only property is not allowed: [S1] ```javascript "use strict"; const obj = {}; Object.defineProperty(obj, "x", {value:0, writable:false}); obj.x = 3.14; // This will cause an error ``` Writing to a get-only property is not allowed: [S1] ```javascript "use strict"; const obj = {get x() {return 0} }; obj.x = 3.14; // This will cause an error ``` Deleting an undeletable property is not allowed: [S1] ```javascript "use strict"; delete Object.prototype; // This will cause an error ``` The word `eval` cannot be used as a variable: [S1] ```javascript "use strict"; let eval = 3.14; // This will cause an error ``` The word `arguments` cannot be used as a variable: [S1] ```javascript "use strict"; let arguments = 3.14; // This will cause an error ``` The `with` statement is not allowed: [S1] ```javascript "use strict"; with (Math){x = cos(2)}; // This will cause an error ``` For security reasons, `eval()` is not allowed to create variables in the scope from which it was called: [S1] ```javascript "use strict"; eval ("var x = 2"); alert (x); // This will cause an error ``` **The "use strict" directive** The `"use strict"` directive is only recognized at the beginning of a script or a function. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's snippets are the applied cases: each shows a construct that runs silently (or wrongly) in normal mode but throws under `"use strict";` β€” undeclared assignment, `delete` on variables/functions, duplicate parameters, octal literals, read-only/get-only writes, reserved words `eval`/`arguments`, `with`, and `eval()` scope injection. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Enable strict mode globally (top of script) (language: JavaScript): ```javascript "use strict"; x = 3.14; // Error: x is not declared ``` Enable strict mode for a single function: ```javascript function myFunction() { "use strict"; // strict-mode code here } ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. The page notes that older browsers/JavaScript versions simply ignore the `"use strict";` string, so it is backward-compatible. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.88 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Scope]], [[JavaScript Hoisting]], [[JavaScript var let const]], [[JavaScript Best Practices]] - **μ°Έμ‘° λ§₯락:** Referenced when hardening JavaScript against silent errors and accidental globals. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Strict Mode β€” https://www.w3schools.com/js/js_strict.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Strict Mode" page (Astra wiki-curation, P-Reinforce v3.1 format).