Skip to main content

JavaScript Variables

JavaScript Fundamentals: JavaScript Variables

What is the definition of a variable in JavaScript?

View Answer:
Interview Response: A variable is a “named storage” for data. We can use variables to store values, visitor information, and other data.

What are the keywords used to declare a variable in JavaScript?

View Answer:
Interview Response: The keywords used to declare a variable include let, const, and var variables.

Technical Response: There are three variable declaration keywords used in JavaScript. They include let, const, and var variables. In modern JavaScript, development let and const remain recommended to optimize your application.

Can you declare multiple variables on one line?

View Answer:
Interview Response: Yes, although the specification does not advise it for readability and consistency reasons.

Code Example:

// Example: one-line

let user = 'John',
age = 25,
message = 'Hello';

// The multiline variant is a bit longer, but easier to read:

let user = 'John';

let age = 25;

let message = 'Hello';

Is there a way to copy stored data from one variable to another?

View Answer:
Interview Response: Yes, declare two variables and copy data into one—better known as copy by reference.

Code Example:

let hello = 'Hello JavaScript!';

let message;

// copy 'Hello world' from hello into the message

message = hello;

// now two variables hold the same data

alert(hello); // Hello world!
alert(message); // Hello world!

Does JavaScript allow you to change the value of a variable?

View Answer:
Interview Response: Yes, by declaring the variable equals the new value. A const declaration on a variable does not have this capability because it is a constant value.

Code Example:

let message;

message = 'Hello!';

message = 'World!'; // value changed
console.log(message); // returns 'World!'

Can you declare a variable twice in JavaScript?

View Answer:
Interview Response: No, it results in a syntax error.

Code Example:

let message = 'This';

// repeated 'let' leads to an error

let message = 'That'; // SyntaxError: 'message' has already been declared

What are the two limitations of variable names in JavaScript?

View Answer:
Interview Response: The variable must contain only letters, digits, dollar signs, or an underscore. The first character cannot be a number.

Technical Response: In JavaScript, variable names are subject to two limitations. Only letters, numbers, or the symbols $ and an underscore _ may appear in the name, and the first character cannot be a number.

Examples of valid names:

let userName;

let test123;
These names are also valid, but not recommended in the format below:

let $ = 1; // declared a variable with the name "$"
let _ = 2; // and now a variable with the name "_"
alert($ + _); // 3

Can you explain the CamelCase naming method?

View Answer:
Interview Response: CamelCase is a naming method in which a name gets made up of many words combined into a single term. Each word’s initial letter gets capitalized so that the name may be easily read. In JavaScript, we use the lowerCamelCase implementation.

Code Example:

let lowerCamelCase = 'lowerCamelCase';
console.log(lowerCamelCase); // returns lowerCamelCase

What stylization rule gets used in naming multi-word variables?

View Answer:
Interview Response: When the name contains multiple words, lower camelCase is the standard.

Code Example:

let myUserName;

Does variable case matter in JavaScript?

View Answer:
Interview Response: Yes, variables named myUser and myuser are two different variables because variables in JavaScript are case-sensitive.

Code Example:

let myUser = 'Jack';
let myuser = 'Jane';

console.log(myUser === myuser); // returns false
console.log(myUser); // Jack
console.log(myuser); // Jane

Are Non-Latin letters allowed in naming variables?

View Answer:
Interview Response: Yes, but it is not recommended.

Technical Response: Yes, it is possible to use any language, including Cyrillic letters or even hieroglyphs, but it remains a lousy approach and not recommended, according to the MDN.

Code Example: For example, technically, this can be implemented, but it remains a lousy approach and goes against recommendations.

let имя = '...';

let= '...';

Is it possible to declare a variable without let, const, or "Old var"?

View Answer:
Interview Response: Outside of strict mode, it is possible but not encouraged. It is possible to find it in earlier programs.

Code Example: Technically, it is achievable but not recommended in modern JavaScript.

// note: no "use strict" in this example

num = 5; // the variable "num" is created if it didn't exist
alert(num); // 5

What is the difference between var and let in JavaScript?

View Answer:
Interview Response: The distinction is that var is function scoped, whereas let is block scoped. If used outside of a function, it is called a global variable.

Technical Response: We use both var and let for variable declaration in JavaScript. However, the distinction is that var is function scoped and let is block scoped. When compared to let, a variable declared using var is considered defined throughout the program.

Can you reassign a value in a variable declared with const?

View Answer:
Interview Response: No, it results in a syntax error because constants do not allow reassignment.

Code Example:

const myBirthday = '01.09.1969';

myBirthday = '01.01.2001'; // error, can't reassign the constant!

When should you use a constant as an alias in JavaScript?

View Answer:
Interview Response: For constant variable values that are difficult to memorize, we use aliases. Capital letters and underscores should get used in this case.

Code Example: Hexadecimal Colors

const COLOR_RED = '#F00';

const COLOR_GREEN = '#0F0';

const COLOR_BLUE = '#00F';

const COLOR_ORANGE = '#FF7F00';

// ...when we need to pick a color

let color = COLOR_ORANGE;

alert(color); // #FF7F00

What are the advantages of aliasing a constant in JavaScript?

View Answer:
Interview Response: Aliases are easy to remember than numeric identifiers, like hexadecimal numbers.

When should a constant be named in caps, and when should it be named in the usual way?

View Answer:
Interview Response: Capital-named constants only get used as aliases for “hard-coded” values.

Technical Response: Constant discovery happens before code execution (like a hexadecimal value for red). Some constants are calculated at run-time during execution but do not alter after they are assigned. Capital-named constants only get used as aliases for "hard-coded" values.

What are some excellent rules for variable naming conventions?

View Answer:
Interview Response: A variable name should have a clean, obvious meaning, describing the data it stores, like currentUser or newUser. Unlike functions, a variable name should always be camelCase and begin with a noun.

Technical Response:

Some good-to-follow rules are:

  1. Use names that are easy to remember, such as userName or shoppingCart.
  2. If you do not know what you are doing, avoid abbreviations or short names like a, b, and c (Ninja Coder – is not a great approach to creating code).
  3. Make your names as descriptive and straightforward as possible. Two instances of bad names are data and value, and such names are nonsensical, and they may only be used in a coding context to make it apparent what data or value the variable refers to in our code.
  4. Make terms with your team. When a site visitor gets referred to as "user," related variables, currentUser or newUser are utilized rather than currentVisitor or newLadyInRed.
  5. A variable name is always camelCase and should begin with a noun to differentiate variables from functions, which generally should begin with a verb.

What differentiates variable and function naming conventions?

View Answer:
Interview Response: A variable name is always camelCase and should begin with a noun to differentiate variables from functions, which generally should begin with a verb.