zhaoJian's Tech Notes

IT Course JavaScript Basics 037_Literals, Variables, Constants

Learning / JavaScript Basics ~3421 words · 9 min read - views

Literals

Literals are values that appear directly in code, such as numbers, strings, boolean values, etc.

  • Number literals: Such as 5, 3.14.
  • String literals: Such as 'Hello, World!', "JavaScript".
  • Boolean literals: true and false.
  • Object literals: Such as { key: 'value', age: 25 }.
  • Array literals: Such as [1, 2, 3, 4].

Example:

// Number literal
var age = 18;
// String literal
var name = "Zhao Jian";
// Boolean literal
var isStudent = true;
// Array literal
var numbers = [1, 2, 3];
// Object literal
var person = {name: "Zhao Jian", age: 18};

Variables

Variables are containers for storing data. They can be used to “store” literals. The literals stored in variables can be modified at will. Variables can be used to describe literals, and variables are relatively convenient to modify. Variables can store any type of value (in fact, variables don’t store any values, but store the memory address of the value), including literals, return values of other variables, etc. Use the var or let keywords to declare variables.

  • Variables declared with the var keyword are global variables or variables within function scope, and var variables can be declared repeatedly.
  • Variables declared with the let keyword are variables within block scope, and let variables cannot be declared repeatedly.

Example:

// Use var to declare (define) a variable
var age = 18;
// Use let to declare (define) a variable
let name = "Zhao Jian";
// Declare first, then assign
let name;
name = "Zhao Jian";
// Declare multiple variables in one line (statement)
let age = 18, name = "Zhao Jian";
Variable Naming

Variable names are a form of identifier. Variable names are names used to identify the memory location where data is stored. Identifiers are symbols or names used to name variables, functions, classes, etc.

Naming Rules

  • Variable names can only use letters (including non-English letters), numbers, underscores (_), and dollar signs ($).
  • The first character of a variable name cannot be a number.
  • Variable names cannot contain spaces or other special characters.
  • Variable names cannot be the same as JavaScript keywords.

Naming Conventions

  • Camel case: The first word starts with a lowercase letter, and other words start with uppercase letters. For example: userName, firstName
  • Pascal case (upper camel case): All words start with uppercase letters. For example: UserName, FirstName
  • Underscore notation: Words are connected with underscores. For example: user_name, first_name

Valid variable name examples:

let userName;
let hello123;
let age;
let $width;
let _height;
let 名字;

Invalid variable name examples:

let 123abc; // Starts with a number
let user name; // Contains a space
let $#; // Contains special characters
let function; // Same as JavaScript keyword

Constants

Constants are values that cannot (or should not) be modified. Constants can be declared using the const keyword. Constants can only be assigned once, and reassigning will cause an error. It is recommended to use uppercase letters and underscores to name these constants.

Example:

// Use const to declare (define) a constant
const PI = 3.14;
const COLOR_RED = "#F00";
// Attempt to modify a constant
PI = 3.15; // Uncaught TypeError: Assignment to constant variable.
Share:

Comments