IT Course JavaScript Basics 037_Literals, Variables, Constants
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:
trueandfalse. - Object literals: Such as
{ key: 'value', age: 25 }. - Array literals: Such as
[1, 2, 3, 4].
Example:
// Number literalvar age = 18;
// String literalvar name = "Zhao Jian";
// Boolean literalvar isStudent = true;
// Array literalvar numbers = [1, 2, 3];
// Object literalvar 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
varkeyword are global variables or variables within function scope, andvarvariables can be declared repeatedly. - Variables declared with the
letkeyword are variables within block scope, andletvariables cannot be declared repeatedly.
Example:
// Use var to declare (define) a variablevar age = 18;
// Use let to declare (define) a variablelet name = "Zhao Jian";
// Declare first, then assignlet 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 numberlet user name; // Contains a spacelet $#; // Contains special characterslet function; // Same as JavaScript keywordConstants
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 constantconst PI = 3.14;const COLOR_RED = "#F00";
// Attempt to modify a constantPI = 3.15; // Uncaught TypeError: Assignment to constant variable.