Computer Science
Javascript Data Types
JavaScript data types include primitive types such as numbers, strings, booleans, null, and undefined, as well as complex types like objects and functions. These data types determine the kind of data that can be stored and manipulated in JavaScript programs. Understanding data types is essential for effectively working with variables and performing operations in JavaScript.
Written by Perlego with AI-assistance
Related key terms
1 of 5
7 Key excerpts on "Javascript Data Types"
- 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 - 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
- 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 - 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: - eBook - PDF
- Alvin Albuero De Luna(Author)
- 2023(Publication Date)
- Arcler Press(Publisher)
7.2. PRIMITIVE DATA TYPES Primitive types of data are those that have been not measured in terms of other kinds. A collection of basic data types is offered by almost all computer languages. The majority of the basic types, for instance, are essentially reflections of both the hardware. Others can be implemented with just a little amount of nonhardware assistance. The basic data kinds of languages are combined with one or more category constructors to describe the structured types (Figure 7.2) (Bruce, 1996; Sekiyama et al., 2015). Figure 7.2. Fundamental data types. Source: https://codeforwin.org/2017/08/data-types-in-c-programming.html. 7.2.1. Numeric Types Primitive types in some earlier computer languages were merely numbers. Among some of the types offered by modern languages, numerical types continue to be at the center of things. 7.2.1.1. Integer An integer is the most popular data type for basic numbers. Numerous systems’ hardware allows integers in a variety of sizes. Some programers offer these integer values as well as sometimes a few more. For instance, Java offers the bytes, short, int, and large verified integer types. Integer variable types, or types for decimal numbers without signed, are included in several languages, such as C++ and C#. Binary code often uses unsigned Programming Language Theory 192 types. Computers store signed integer values as strings of bits, including one bit (usually the leftmost) serving as that of the sign. The hardware natively supports the majority of integer types (Bobrow and Raphael, 1974; Ernst and Ogden, 1980). The Python large integer type (F# also offers such integers) is one illustration of such an integer data type which is not funded either by hardware. - 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 - eBook - PDF
- M Smit R Jonker(Author)
- 2017(Publication Date)
- Macmillan(Publisher)
79 Module 10 Topic 3: Computer data storage Computer data types Module 10 Overview At the end of this module, you should be able to: • Unit 10.1: Distinguish between data types with examples and the way the data is stored in memory. • Unit 10.2: Distinguish between categories of coding systems and their uses in a business environment. • Unit 10.3: Explain and illustrate how data manipulation operations are performed on data types. Unit 10.1: Data types and the way data is stored in memory Data is classified into various types of information. For humans, it is easy to see the difference between data types, but in the case of computers you need to declare (specify) what data type it is. Computers use special internal codes to keep track of different types of data. 10.1.1 Data types A computer program consists of two elements: data and operations on that data. Computers can only represent very simple pieces of information. All complex information must be built up from basic data types. We will now discuss the most common data types. a) Characters and strings A character is a letter, number or symbol. The character data type contains only one character. A string is a linear (continuous) sequence of characters, words or other data that the user can input from the keyboard. This includes the letters of the alphabet, numbers, punctuation and other special characters. Example 10.1: Strings Each of the following is a string: “My name is Lebo.” “My name is Lebo and I am 20.” “My name is Lebo and I was born on 12 January 1970.” “(011) 812 4545” character: a letter, number or symbol string: a linear sequence of characters, words or other data that the user can input from the keyboard Words & Terms 80 Module 10 As you can see from Example 10.1, a string contains all sorts of characters that can be entered via the keyboard. The string is captured between double quotes. It is not possible to do calculations with numbers in 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.






