Part 1: Variables and Constants
When should you use let, const, and var in modern JavaScript?
- Use let when the variable's value will change. -Use const when the value will not change. -Avoid var unless necessary for older code compatibility.
How do you create and update a variable using let?
- declare with let: let fridge = "empty" =Update the value: fridge = "full";
How do you create and update a variable using var?
-Declare with var: var favoriteShow = Shameless"; -Update without re-declaring var: favoriteShow = "New Show";
What are the best practices for declaring and naming variables in JavaScript?
-Use let or const instead of var. -Use meaningful, descriptive names. -Follow the CamelCase convention - Be mindful of scope to avoid errors. (Global, function, and Block scope)
What are some important rules for naming variables and constants in JavaScript?
1. Variable name are case-sensitive. 2. Cannot begin with a number. 3. Cannot contain symbols (except _ or $) 4. Camelcase is preferred for multi-word names. 5. Start variable names with a lowercase letter. Example:
What are some examples of valid and invalid variable names?
Valid= let user; let applicant; , let fav-candy;
Are variable names case-sensitive in JavaScript?
Yes, let a = "hello" and let A = "goodbye" are two different variables.
How is const different from let?
const is used to declare variables whose values cannot be reassigned after the initial assignment. It is also blocked-scoped.
What is the difference between let and var?
let is block-scoped (local to the block it's declare in), whereas var is function-scoped. Use let when you expect to change the value of a variable.
What is the difference between var and other variable declarations like let and const?
var allows variables to be reassigned, but it has a function-scoped behavior, which can lead to confusion. It's generally considered outdated, and let or const are preferred.
What is the purpose of a variable in JAvaScript?
A variable stores data to be reused. Data in JavaScript in stored in variables and can be manipulated using reserved keywords like var, let, and const.
What is the CamelCase naming convention for variables?
CamelCase connects multiple words, with each word's first letter capitalized except for the first word. (e.g, let userName)
Can you reassign a value to a const variable?
No. Attempting to reassign a const variable will result in a "TypeError"
