Chapter 2. Types, Values, and Variables
Computer programs work by manipulating values, such as the number 3.14 or the text âHello World.â The kinds of values that can be represented and manipulated in a programming language are known as types. When a program needs to retain a value for future use, it assigns the value to (or âstoresâ the value in) a variable. A variable defines a symbolic name for a value and allows the value to be referred to by name.
JavaScript types can be divided into two categories: primitive types and object types. JavaScriptâs primitive types include numbers, strings of text (known as strings), and Boolean truth values (known as booleans). The first few sections of this chapter explain JavaScriptâs primitive types. (Chapters 5, 6, and 7 describe three kinds of JavaScript object types.)
JavaScript converts values liberally from one type to another. If a program expects a string, for example, and you give it a number, it will automatically convert the number to a string for you. If you use a nonboolean value where a boolean is expected, JavaScript will convert accordingly. Type Conversions describes JavaScriptâs type conversions.
JavaScript variables are untyped: you can assign a value of any
type to a variable, and you can later assign a value of a different type to
the same variable. Variables are declared with the
var
keyword. JavaScript
uses lexical scoping. Variables declared
outside of a function are global variables and are visible everywhere ...
Get JavaScript Pocket Reference, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.