deno fmt helps you keep your code neat and easy to read by fixing spacing and indentation automatically.
deno fmt for code formatting
deno fmt [options] [files...]
You can run it without arguments to format all supported files in the current folder.
Use file paths or glob patterns to format specific files.
deno fmt
mod.ts and all TypeScript files in the utils folder.deno fmt mod.ts utils/*.tsdeno fmt --check
This example shows a small unformatted TypeScript function. Running deno fmt fixes spacing, adds missing semicolons, and formats the code nicely.
/* Unformatted code example */ function greet (name:string){console.log("Hello, " +name)} greet("Deno") // Run in terminal: deno fmt /* After formatting, code looks like: */ function greet(name: string) { console.log("Hello, " + name); } greet("Deno");
deno fmt supports TypeScript, JavaScript, JSON, and more file types.
You can configure formatting rules with a deno.json file if needed.
Running deno fmt is safe and won't change your code logic, only style.
deno fmt automatically formats your code for readability and consistency.
Use it often to keep your project clean and avoid style debates.
It works on many file types and can check formatting without changing files.
Practice
deno fmt command?Solution
Step 1: Understand the role of
deno fmtdeno fmtis designed to format code automatically, making it easier to read and consistent across files.Step 2: Differentiate from other commands
Running programs, installing modules, or security checks are done by other commands likedeno run,deno install, ordeno lint.Final Answer:
To automatically format your code for better readability and consistency -> Option CQuick Check:
deno fmt formats code [OK]
- Confusing fmt with run or install commands
- Thinking fmt checks code errors instead of formatting
- Assuming fmt modifies program logic
deno fmt on all supported files in your project?Solution
Step 1: Identify the basic command to format files
Runningdeno fmtwithout extra flags formats all supported files in the current directory and subdirectories.Step 2: Understand other options
--checkonly checks formatting without changing files;runandinstallare unrelated commands.Final Answer:
deno fmt -> Option AQuick Check:
Basic command to format = deno fmt [OK]
- Adding unnecessary flags like run or install
- Using --check when wanting to format files
- Confusing fmt with other Deno commands
example.ts:
function greet ( name:string ){console.log( "Hello, " +name );}
What will be the output in example.ts after running deno fmt?Solution
Step 1: Understand what
deno fmtdoes to code styledeno fmtadds spaces after commas, spaces around operators, and proper indentation and line breaks for readability.Step 2: Compare options with expected formatting
function greet(name: string) { console.log("Hello, " + name); } shows proper spacing after parameters, braces on new lines, and indentation inside the function, matchingdeno fmtstyle.Final Answer:
function greet(name: string) { console.log("Hello, " + name); } -> Option BQuick Check:
deno fmt adds spaces and indentation [OK]
- Ignoring spaces after colons and commas
- Not indenting inside function blocks
- Keeping all code on one line
deno fmt --check on your project and get an error saying some files are not formatted. What should you do to fix this?Solution
Step 1: Understand what
This command checks if files are formatted but does not change them.deno fmt --checkdoesStep 2: Fix formatting issues
To fix formatting, rundeno fmtwithout flags to automatically format all files.Final Answer:
Run deno fmt without flags to format the files automatically -> Option AQuick Check:
Use deno fmt to fix formatting [OK]
- Ignoring formatting errors thinking they don't matter
- Trying to fix formatting by running code
- Deleting files instead of formatting
Solution
Step 1: Identify the command that formats code automatically
deno fmtformats all supported files, making it ideal for pre-commit hooks to keep code clean.Step 2: Understand other commands' purposes
deno runexecutes scripts,deno lintchecks code style but does not fix formatting, anddeno installinstalls scripts.Final Answer:
deno fmt -> Option DQuick Check:
Pre-commit formatting uses deno fmt [OK]
- Using deno run or deno lint instead of fmt
- Thinking lint fixes formatting automatically
- Not automating formatting before commits
Ready to prove it?
Pass the test and this pattern moves unknown
