💬 Debugging & Tutorials

What is the correct way to declare a variable in javascript?

A

Anthony Olajide

Jan 2, 2026 at 2:56 PM

4 replies 257 views
a. const cityName = Lagos;

b. const cityname = "Lagos";

c. const cityName = "Lagos";

d. const CityName = "Lagos";

Image

4 Replies

Sign in to join the conversation

d

diltony@yahoo.com

5 days ago
[[29,37],[9]]
b

blessing@africoders.com

5 days ago
Out of the options you provided, the most correct way to declare a variable in JavaScript for the city name is:

c. const cityName = "Lagos";

Here's why:

const: This keyword is used to declare a constant variable. This means the value assigned to the variable cannot be changed later in the code. This is a good practice for things like city names that are unlikely to change.
cityName: This is a clear and descriptive variable name. It uses camelCase (lowercase for the first word and uppercase for subsequent words) which is the convention for variable names in JavaScript.
"Lagos": This is a string literal enclosed in double quotes, which is the appropriate way to store text data like a city name.
b

blessing@africoders.com

5 days ago
There are actually two main ways to declare variables in modern JavaScript: [s]`[/s]let `and [s]`[/s]const`. Both are block-scoped, meaning their scope is limited to the block of code they are declared in (like an if statement or a function).

Using [s]`[/s]let `allows you to reassign a value to the variable later in your code. This is useful for variables that will change throughout your program.

On the other hand,[s]` [/s]const` is used for variables that should hold a constant value and not be reassigned. This helps prevent accidental modification and improves code readability.

Here's an example:

[s]```[/s]
let name = "Alice"; // Declares a variable with let and assigns the value "Alice"
name = "Bob"; // Reassigns the value of name to "Bob"
const PI = 3.14159; // Declares a constant with const and assigns the value of pi
// PI = 3; This would cause an error because PI is constan
```
Generally, it's recommended to use const by default and only use let when you specifically need to reassign the variable. This helps prevent bugs and makes your code more predictable.
A

Anthony Olajide

5 days ago
@"blessedtechie"#p521 you are spot on.