Computer Science
Javascript Reference Data Types
Javascript Reference Data Types are the building blocks of any programming language. These data types include numbers, strings, booleans, null, undefined, objects, and symbols. Understanding these data types is essential for writing effective and efficient Javascript code.
Written by Perlego with AI-assistance
Related key terms
1 of 5
6 Key excerpts on "Javascript Reference Data Types"
- No longer available |Learn more
- Matt Frisbie(Author)
- 2019(Publication Date)
- Wrox(Publisher)
5 Basic Reference TypesWHAT'S IN THIS CHAPTER?
- Working with objects
- Understanding basic JavaScript data types
- Working with primitives and primitive wrappers
WROX.COM DOWNLOADS FOR THIS CHAPTER
Please note that all the code examples for this chapter are available as a part of this chapter's code download on the book's website at www.wrox.com/go/projavascript4e on the Download Code tab.A reference value (object) is an instance of a specific reference type. In ECMAScript, reference types are structures used to group data and functionality together and are often incorrectly called classes. Although technically an object-oriented language, ECMAScript lacks some basic constructs that have traditionally been associated with object-oriented programming, including classes and interfaces. Reference types are also sometimes called object definitions because they describe the properties and methods that objects should have.NOTE Even though reference types are similar to classes, the two concepts are not equivalent. To avoid any confusion, the term “class” is not used in the rest of this chapter.Again, objects are considered to be instances of a particular reference type. New objects are created by using the new operator followed by a constructor. A constructor is simply a function whose purpose is to create a new object. Consider the following line of code:let now = new Date();This code creates a new instance of the Date reference type and stores it in the variable now . The constructor being used is Date() , which creates a simple object with only the default properties and methods. ECMAScript provides a number of native reference types, such as Date , to help developers with common computing tasks.NOTE Functions are a reference type, but they are too broad of a topic for this chapter and therefore have an entire chapter devoted to them. Refer to the Functions chapter.THE DATE TYPE
The ECMAScript Date type is based on an early version of java.util.Date from Java. As such, the Date type stores dates as the number of milliseconds that have passed since midnight on January 1, 1970 UTC (Universal Time Code). Using this data storage format, the Date - No longer available |Learn more
The JavaScript Workshop
A New, Interactive Approach to Learning JavaScript
- Joseph Labrecque, Jahred Love, Daniel Rosenbaum, Nick Turner, Gaurav Mehla, Alonzo L. Hosford, Florian Sloot, Philip Kirkbride(Authors)
- 2019(Publication Date)
- Packt Publishing(Publisher)
if/else statements to apply conditions to different variables, this chapter will be one of the most important stepping stones in your JavaScript learning path. A detailed understanding of Booleans, strings, objects, arrays, functions, arguments, and so on will improve your development skills.Data Types
Programming is all about manipulating data. Data can represent values such as people's names, temperature, image dimensions, the amount of disk storage, and total likes on a discussion group post.All the data in a program has a data type. The data types that you usually learn to use first in JavaScript are number, string, boolean, object, array, and function. The number, string, and Boolean data types represent a single value. Objects represent more complex data. Functions are for writing programs.Some common JavaScript data types with their uses and descriptions are as follows:- number : Any positive or negative value whole numbers, usually called integers and floating-point numbers, that can be used in mathematical operations. It is used in product prices, checkout totals, the number of likes on a post, the geometry value of Pi, and can be used as a random number.
- string : Any set of valid characters that cannot be, or are not intended to be, used in computational operations. They are used to comment on a discussion post which can be a company name, a street address, name of a place, an account number, a telephone number, or a postal number.
- boolean : Any value representing true and false. It is used to check whether a form can be submitted, whether a password meets its required characters, whether an order balance qualifies for free shipping, and whether a button can be clicked.
- object : An unordered collection of values, called properties, and code, called methods, that are intended to work together. It is used for real-world objects such as an order, stopwatch, clock, date, or microwave. They can be used for software objects such as a web page document, an HTML element on a web page, a CSS style rule, or an HTTP request.
- function
- eBook - ePub
- Jeremy McPeak(Author)
- 2015(Publication Date)
- Wrox(Publisher)
In this chapter you start by looking at the various types of data JavaScript can process. Then you look at how you can store this data in the computer’s memory so you can use it again and again in the code. Finally, you see how to use JavaScript to manipulate and process the data.TYPES OF DATA IN JAVASCRIPT
Data can come in many different forms, or types. You’ll recognize some of the data types that JavaScript handles from the world outside of programming—for example, numbers and text. Other data types are a little more abstract and are used to make programming easier; one example is the object data type, which you won’t see in detail until Chapter 5.Some programming languages are strongly typed. In these languages, whenever you use a piece of data, you need to explicitly state what sort of data you are dealing with, and use of that data must follow strict rules applicable to its type. For example, in a strongly typed language you can’t add a number and a word.JavaScript, on the other hand, is a weakly typed language and a lot more forgiving about how you use different types of data. When you deal with data, you often don’t need to specify type; JavaScript will work that out for itself. Furthermore, when you are using different types of data at the same time, JavaScript will work out behind the scenes what it is you’re trying to do.Given how easygoing JavaScript is about data, why talk about data types at all? Why not just cut to the chase and start using data without worrying about its type?First of all, although JavaScript is very good at working out what data it’s dealing with, on occasion it’ll get things wrong or at least not do what you want it to do. In these situations, you need to make it explicit to JavaScript what sort of data type you intended and how it should be used. To do that, you first need to know a little bit about data types.A second reason is that data types enable you to use data effectively in your code. The things that you can do with data and the results you’ll get depend on the type of data being used, even if you don’t explicitly specify what type it is. For example, although trying to multiply two numbers makes sense, doing the same thing with text doesn’t. Also, the result of adding numbers is very different from the result of adding text. With numbers you get the sum, but with text you get one big piece of text consisting of the other pieces joined together. - eBook - PDF
- Patrick Carey, Sasha Vodnik, Patrick Carey(Authors)
- 2021(Publication Date)
- Cengage Learning EMEA(Publisher)
CHAPTER 2 WORKING WITH FUNCTIONS, DATA TYPES, AND OPERATORS 48 Working with Data Types Variables can contain many different kinds of values such as the time of day, a dollar amount, or a person’s name. A data type is the specific category of information that a variable contains. The data type determines how much memory is allocated for the data stored in the variable. The data type also governs the kinds of operations that can be performed on the variable. Data types that can be assigned only a single value are called primitive types. JavaScript supports the five primitive data types described in Figure 2-6. The JavaScript language also supports the object data type used for creating a collection of properties. You will learn about the object type in a later chapter. Note DATA TYPE DESCRIPTION number A positive or negative number with or without decimal places, or a number written using exponential notation Boolean A logical value of true or false string Text such as “Hello World!" undefined An unassigned, undeclared, or nonexistent value null An empty value Figure 2-6 Primitive JavaScript data types You might be confused about the distinction between a null value and an undefined value. Null is both a data type and a value. You assign the null value to a variable to indicate that the variable does not contain any data. In contrast, an undefined variable is a variable that has never had a value assigned to it, has not been initialized or does not even exist. For example, a variable that has been declared but not given an initial value is undefined but not null. Many programming languages require that you declare the type of data that a variable contains. Such languages are called strongly typed programming languages. A strongly typed language is also known as statically typed, because data types cannot be changed after they have been declared. - eBook - ePub
LPI Web Development Essentials Study Guide
Exam 030-100
- Audrey O'Shea(Author)
- 2023(Publication Date)
- Sybex(Publisher)
A value's data type determines what can and cannot be done with that data. In JavaScript, there's no need to declare the type of data when it is entered, because JavaScript is a dynamically typed language that automatically determines the variable's data type for you. Although this makes writing code much easier, it can occasionally lead to bugs related to data type when the code is run.Primitive Types
Primitive data types are built into most programming languages. They are basic in nature and may be used to build more sophisticated data types, as you'll see in the next section. JavaScript has eight primitive data types, shown in Table 14.2 .Primitive data typesTABLE 14.2Type Description Boolean Logical, true or false, 1 or 0. BigInt Big integer, a number beyond what's considered “safe.” Cannot be fractional numbers. Null Empty, but defined. Number Positive floating point (decimal anywhere) numbers between 2–1074 and 21024 . String Alphanumeric characters not treated as numbers. Symbol Creates a unique symbol for a value within a program. Undefined The absence of a value—for example, a variable defined without its value (let x; ) creates the variable but with no value assigned. A variable's value type within a JavaScript program can change as its value changes. For example, let a = "apple"; assigns the string type to variable a. Later in the program, let a = 10; will change a 's type to number. Changing it again with a statement like let a = true returns the variable type of Boolean. If however, you write the statement let a = "true"; or let a = "10"; , then a will return the variable type of string. The difference between a = true and a = "true" is the quotes. The quotes turn the Boolean value of logical true to a text string.At any time you can tell what type a variable is using the typeof command. For example, let a = "apple"; console.log(typeof a); outputs string on the console log because the quotes tell the program that what's inside is a string.Data types null and undefined are special. They might look the same at first glance, but they are not. Neither null nor undefined is equal to zero. undefined means that the variable has been declared, but no value has been assigned. For example, let b; console.log b; - eBook - ePub
JavaScript from Beginner to Professional
Learn JavaScript quickly by building fun, interactive, and dynamic web apps, games, and pages
- Laurence Lars Svekis, Maaike van Putten, Rob Percival, Laurence Svekis, Codestars By Rob Percival(Authors)
- 2021(Publication Date)
- Packt Publishing(Publisher)
- Variables cannot contain spaces, but they can use underscores. If you use a space, JavaScript doesn't recognize it as a single variable.
The value of your variable can be anything. Let's start with the easiest thing variables can be: primitives.We will be using camel case here. This means that when we want to use multiple words to describe a variable, we will start with a lowercase word, then use a capital for every new word after the first word—for example: ageOfBuyer .Whatever the convention is in the place you are working, the key is consistency. If all naming is done in a similar format, the code will look cleaner and more readable, which makes it a lot easier to make a small change later.Primitive data types
Now you know what variables are and why we need them in our code, it is time to look at the different types of values we can store in variables. Variables get a value assigned. And these values can be of different types. JavaScript is a loosely typed language. This means that JavaScript determines the type based on the value. The type does not need to be named explicitly. For example, if you declared a value of 5, JavaScript will automatically define it as a number type.A distinction exists between primitive data types and other, more complex data types. In this chapter, we will cover the primitive type, which is a relatively simple data structure. Let's say for now that they just contain a value and have a type. JavaScript has seven primitives: String, Number, BigInt, Boolean, Symbol, undefined, and null. We'll discuss each of them in more detail below.String
A string is used to store a text value. It is a sequence of characters. There are different ways to declare a string:
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.





