Bird
Raised Fist0
Denoframework~10 mins

Environment variable permissions in Deno - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read an environment variable named 'API_KEY'.

Deno
const apiKey = Deno.env.[1]('API_KEY');
Drag options to blanks, or click blank then click option'
Alist
Bdelete
Cget
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'get' to read a variable.
Trying to list variables when only one is needed.
2fill in blank
medium

Complete the code to set an environment variable 'MODE' to 'production'.

Deno
Deno.env.[1]('MODE', 'production');
Drag options to blanks, or click blank then click option'
Alist
Bget
Cdelete
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'set' to assign a value.
Trying to delete or list variables instead of setting.
3fill in blank
hard

Fix the error in the code to delete the environment variable 'TEMP_VAR'.

Deno
Deno.env.[1]('TEMP_VAR');
Drag options to blanks, or click blank then click option'
Aremove
Bdelete
Cunset
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'remove' or 'unset' which are not valid Deno methods.
Trying to use 'clear' which removes all variables.
4fill in blank
hard

Fill both blanks to check if the environment variable 'DEBUG' exists and then read it.

Deno
if (Deno.env.[1]('DEBUG')) {
  const debugValue = Deno.env.[2]('DEBUG');
}
Drag options to blanks, or click blank then click option'
Aget
Bhas
Cexists
Dcheck
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' to check existence, which returns undefined if missing.
Using non-existent methods like 'exists' or 'check'.
5fill in blank
hard

Fill all three blanks to safely read 'PORT' with a default of '3000' if not set.

Deno
const port = Deno.env.[1]('PORT') ?? [2];
const portNumber = Number([3]);
Drag options to blanks, or click blank then click option'
Aget
B'3000'
Cport
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'get' to read the variable.
Not providing a default value causing errors.
Converting the wrong variable to number.

Practice

(1/5)
1. What does the --allow-env flag do in Deno?
easy
A. It encrypts environment variables.
B. It grants permission to access environment variables.
C. It sets environment variables automatically.
D. It disables environment variable access.

Solution

  1. Step 1: Understand the purpose of --allow-env

    This flag is used to allow a Deno program to read environment variables, which are otherwise blocked by default for security.
  2. Step 2: Identify the correct description

    Among the options, only granting permission to access environment variables matches the flag's purpose.
  3. Final Answer:

    It grants permission to access environment variables. -> Option B
  4. Quick Check:

    Permission flag for env vars = D [OK]
Hint: Remember: --allow-env means allow environment access [OK]
Common Mistakes:
  • Thinking it disables environment access
  • Confusing it with setting variables
  • Assuming it encrypts variables
2. Which of the following is the correct way to allow access only to the environment variable API_KEY in Deno?
easy
A. deno run --allow-env=API_KEY script.ts
B. deno run --allow-env API_KEY script.ts
C. deno run --allow-env API_KEY=script.ts
D. deno run --allow-env:API_KEY script.ts

Solution

  1. Step 1: Recall the syntax for environment variable permission

    Deno uses --allow-env=VAR_NAME to allow access only to a specific variable.
  2. Step 2: Match the correct syntax

    deno run --allow-env=API_KEY script.ts correctly uses --allow-env=API_KEY. Other options misuse spaces, equal signs, or colons.
  3. Final Answer:

    deno run --allow-env=API_KEY script.ts -> Option A
  4. Quick Check:

    Specific env var permission uses = [OK]
Hint: Use = to specify variable after --allow-env [OK]
Common Mistakes:
  • Using space instead of = after --allow-env
  • Adding extra characters like : or = in wrong places
  • Confusing argument order
3. Given this command: deno run --allow-env=HOME script.ts and this code inside script.ts:
console.log(Deno.env.get("HOME"));
console.log(Deno.env.get("PATH"));
What will be the output?
medium
A. Prints undefined for both variables.
B. Prints HOME variable value, then prints PATH variable value.
C. Throws a permission error on accessing PATH.
D. Prints HOME variable value, then prints undefined.

Solution

  1. Step 1: Understand permission scope

    The command allows access only to the HOME environment variable, not PATH.
  2. Step 2: Predict output for each variable

    Accessing HOME prints its value. Accessing PATH returns undefined because permission is not granted.
  3. Final Answer:

    Prints HOME variable value, then prints undefined. -> Option D
  4. Quick Check:

    Allowed var prints value, others undefined [OK]
Hint: Only allowed env vars return values; others give undefined [OK]
Common Mistakes:
  • Assuming all env vars print values
  • Expecting a permission error instead of undefined
  • Confusing variable names
4. You run deno run --allow-env=USER script.ts but inside script.ts you try to access Deno.env.get("HOME"). What will happen?
medium
A. It returns undefined without error.
B. It prints the HOME variable value.
C. It throws a runtime permission error.
D. It crashes the program.

Solution

  1. Step 1: Check permission granted

    Only USER variable access is allowed, not HOME.
  2. Step 2: Behavior when accessing disallowed env var

    Deno returns undefined for disallowed environment variables instead of throwing errors.
  3. Final Answer:

    It returns undefined without error. -> Option A
  4. Quick Check:

    Disallowed env vars return undefined, no error [OK]
Hint: Disallowed env vars return undefined, no crash [OK]
Common Mistakes:
  • Expecting a permission error
  • Thinking it prints the value anyway
  • Assuming program crashes
5. You want to run a Deno script that needs access to API_KEY and DB_PASS environment variables only. Which command correctly grants minimal permissions?
hard
A. deno run --allow-env=API_KEY script.ts && deno run --allow-env=DB_PASS script.ts
B. deno run --allow-env script.ts
C. deno run --allow-env=API_KEY,DB_PASS script.ts
D. deno run --allow-env=API_KEY DB_PASS script.ts

Solution

  1. Step 1: Understand how to specify multiple env vars

    Deno allows multiple variables by comma-separating them after --allow-env=.
  2. Step 2: Evaluate each option

    deno run --allow-env=API_KEY,DB_PASS script.ts correctly uses comma separation. deno run --allow-env script.ts grants all env vars (too broad). deno run --allow-env=API_KEY script.ts && deno run --allow-env=DB_PASS script.ts runs script twice separately (inefficient). deno run --allow-env=API_KEY DB_PASS script.ts syntax is invalid.
  3. Final Answer:

    deno run --allow-env=API_KEY,DB_PASS script.ts -> Option C
  4. Quick Check:

    Multiple vars comma-separated after --allow-env= [OK]
Hint: Use commas to list multiple env vars after --allow-env= [OK]
Common Mistakes:
  • Granting all env vars instead of specific ones
  • Running script multiple times instead of once
  • Using spaces instead of commas
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard