Computer Science
Javascript Primitive Data Types
JavaScript primitive data types are the basic building blocks of the language and include numbers, strings, booleans, null, undefined, and symbols. These data types are immutable and directly hold a value. They are not objects and do not have methods. Primitive data types are stored by value, meaning that when they are assigned to a variable, the actual value is copied.
Written by Perlego with AI-assistance
Related key terms
1 of 5
10 Key excerpts on "Javascript Primitive Data Types"
- 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
Clean Code in JavaScript
Develop reliable, maintainable, and robust JavaScript
- James Padolsey(Author)
- 2020(Publication Date)
- Packt Publishing(Publisher)
Primitive and Built-In Types
So far, we have explored the meaning of clean code from several different perspectives. We've explored how the code we write allows our users to wield remarkable complexity by leveraging abstractions. We've gone on to discuss the tenets of clean code, such as reliability and usability, and the various traps and challenges to watch out for when pursuing these goals.In this chapter, we'll be exploring the JavaScript language itself, in great detail, including both the more common language constructs and the more obscure and confusing aspects. We'll be applying our accrued wealth of knowledge about clean code to all these parts of the language and will build an understanding of JavaScript that's tailored purely to the creation of clean code.We'll begin by looking at the most atomic part of JavaScript: the primitive values that serve as the building blocks for any program. Then, we'll move on to non-primitive values, known as objects . In our exploration of these types, we will, through examples, be exposing the semantics that make each type unique and the pitfalls to avoid in their usage. The crucial knowledge that we'll gain in this chapter will be applied in later chapters as we build up a truly complete knowledge of what it means to write clean code in JavaScript.By the end of this chapter, you should feel comfortable in the following topic areas:- Primitive types
- Objects
- Functions
- Arrays and iterables
- Regular expressions
Passage contains an image
Primitive types
A primitive type in JavaScript is any value that is not an object and thus does not have any methods or properties. There are seven primitive types in JavaScript:- Number
- String
- Boolean
- Undefined
- Null
- BigInt
- Symbol
In this section, we'll explore the common characteristics among these primitives and delve into each individual type to explore how it works and what potential hazards exist in its usage. We'll gain an appreciation for how the JavaScript language itself is just a set of distinct abstractions that, when wielded masterfully, can make easy work of any problem domain. - 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
- Chris Minnick(Author)
- 2023(Publication Date)
- For Dummies(Publisher)
68 BOOK 1 JavaScript Fundamentals As you’ll see when you start working with JavaScript objects, arrays, and modules, there are exceptions to every rule. Taking a Look at the Data Types Variables and constants all have two things in common: a value and a type. You’ve already seen examples of values. These are the bits of data that you want to store in the variable. In this section, I talk about type. A variable’s data type is the kind of data a variable can hold. It’s what determines whether 97103-4534 is a postal code or a mathematical operation that results in 92569. JavaScript is loose and dynamic JavaScript is a loosely typed language. What this means is that you can store any type of data in a variable or constant without having to tell JavaScript in advance the type of data you’ll store in the variable. JavaScript is also a dynamically typed language. This means that you can change the type of data stored in a variable. You might initialize a variable using a number but later store text in it. Though changing the type of a variable is unusual and generally should be avoided, JavaScript tries to be friendly and doesn’t complain. To see a value’s or variable’s data type, you can use the typeof operator. To try it out, open your JavaScript console and try entering the following expressions: » typeof "1" » typeof 0 » typeof true » typeof "true" » typeof a » typeof "a" The result of running these expressions is shown in Figure 3-3. Using Data CHAPTER 3 Using Data 69 Passing by value JavaScript has seven basic data types, which are known as the primitive data types. Primitive data types are passed by value. When you create a new variable from an existing variable, what’s happening is that the value of the existing vari- able is copied (as if you were taking a picture of it) to the new one. Let’s look at an example to better understand the implications of this concept. - 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)
5. Beyond the Fundamentals
OverviewBy the end of this chapter, you will be able to identify the difference between JavaScript's mutable and immutable types; manipulate each of the built-in data types confidently; convert data from one type to another; format data types for presentation; and differentiate between an expression and a statement.Introduction
In the previous chapter, you were given a tour of JavaScript, its runtimes, and its history. Using a high-level topography, that chapter will have given you an idea as to what JavaScript is, what it can do, and its ubiquity within the internet software development industry.Understanding code can be difficult for beginners. JavaScript is no exception. Its flexibility, extensive language syntax, and varying coding patterns can prove daunting to the uninitiated.This chapter will take you a step closer to writing your own software applications in JavaScript. By explaining the fundamentals, you will be empowered to not only understand what scripts do, but how to reason about problems using JavaScript syntax.In this chapter, you will take a close look at JavaScript's type system. All programming languages have a type system. Types literally dictate the type of data stored in a variable or function parameter. Types are typically separated into two categories: primitive and complex types.In JavaScript, all primitive data types are immutable. This means that the value cannot be changed in memory. New values can be assigned to a variable, but the underlying data stored in memory cannot be modified directly. This differs from the case in languages such as C++, where values can be directly altered in memory using pointers and helper functions. In JavaScript, when passing a primitive value from one variable to another, the data is copied in memory to the new variable. Therefore, updating one variable does not affect the other.Complex data types work differently. They are also known as reference types. Reference types include the Object type and all of its derivatives, such as Array , Date , and Function - eBook - ePub
- David Matuszek(Author)
- 2023(Publication Date)
- Chapman and Hall/CRC(Publisher)
Chapter 2 JavaScript The Bare MinimumDOI: 10.1201/9781003359609-2This chapter and Chapter 3 describe JavaScript simply as a language, without reference to Web programming. It is in two major parts:The Bare Minimum—This section is intended to get you started programming in JavaScript as quickly as possible. The best way to do that is to try things out as you go.In More Detail—This goes over the same material again, filling in a lot of the gaps. To some extent, it can also be used as a reference.2.1 Comments
// introduces a comment that extends to the end of the line.Multi-line comments start with /* and end with */ .Inside a comment, // and /* have no special meaning (so you cannot “nest” comments). Inside a quoted string or regular expression, // and /* do not start a comment.2.2 Data Types
2.2.1 Primitives
There are eight data types:- A number may be written with or without a decimal point, but all numbers are stored as double precision floating point.
- A bigint is an integer with an arbitrarily large number of digits.
- The two boolean values are written as true and false .
- A string may be enclosed in either single quotes, double quotes, or backticks (`). There is no “character” type.
- A symbol is a value that is guaranteed to be unique; no other value is equal to it. A symbol is created by calling Symbol() or Symbol( name) , where name is a (not necessarily unique) value that may be helpful in debugging.
- The undefined type has a single value, undefined . It is the value of a variable that has been declared but not yet given a value.
- The null type has a single value, null , meaning that the value does not exist.
- An object is any more complicated data type. Functions, arrays, sets, maps, regular expressions, errors, and dates are all special types of object, as are user-defined objects.
The type of a value can be determined by typeof , which can be used as either an operator, typeof x, or as a function, typeof( x) . It will return, as a string, one of the type names given above (for example, "number" - eBook - ePub
- Jeremy McPeak(Author)
- 2015(Publication Date)
- Wrox(Publisher)
2 Data Types and VariablesWHAT YOU WILL LEARN IN THIS CHAPTER:- Representing data in code
- Storing data in memory
- Making calculations
- Converting data
WROX.COM CODE DOWNLOADS FOR THIS CHAPTERYou can find the wrox.com code downloads for this chapter at http://www.wiley.com/go/BeginningJavaScript5E on the Download Code tab. You can also view all of the examples and related files at http://beginningjs.com .One of the main uses of computers is to process and display information. By processing, we mean the information is modified, interpreted, or filtered in some way by the computer. For example, on an online banking website, a customer may request details of all money paid out from his account in the past month. Here the computer would retrieve the information, filter out any information not related to payments made in the past month, and then display what’s left in a web page. In some situations, information is processed without being displayed, and at other times, information is obtained directly without being processed. For example, in a banking environment, regular payments may be processed and transferred electronically without any human interaction or display.In computing, information is referred to as data. Data comes in all sorts of forms, such as numbers, text, dates, and times, to mention just a few. In this chapter, you look specifically at how JavaScript handles data such as numbers and text. An understanding of how data is handled is fundamental to any programming language.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 - 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. - eBook - PDF
Customizing Vendor Systems for Better User Experiences
The Innovative Librarian's Guide
- Matthew Reidsma(Author)
- 2016(Publication Date)
- Libraries Unlimited(Publisher)
All you have to do is declare a variable, and then JavaScript will guess what kind of data you have based on the information you give it. This makes writing code easier and faster, but at times, JavaScript might think that a variable contains a different data type that you’d intended to store, which will cause an error. However, JavaScript data types are dynamic, which means that you can change not only the data stored within a variable, but also the data type itself. That means that at one point in your script, you might have a variable that contains a string of characters, while later it might become a numeric variable. Data types are important because they not only tell you what kind of vari- able you have, but also will affect what you can do with the data in your vari- able. Some JavaScript functions, for instance, will work only on certain types of variables. indexOf(), a method we’ll look at later in this chapter that allows you to search for a pattern of characters within a larger set of data, will not work on numbers. Other JavaScript operators change their function depending on the variable type. The + operator allows you to concatenate two strings of data, but when used with numbers, it serves as an addition operator, adding the two numbers together. Be careful with dynamic data types, since if the data type of your variable changes during your script, you may not be able to predict the behavior of JavaScript operators or functions. JavaScript Basics 29 Strings One of the most common data types in JavaScript is a string. Strings are a series of characters written within quotes. You can use single or double quotes for strings, but if you need to use quotes as part of your string, they cannot match the quotes that surround the variable. For instance, this string variable is valid: var myString = 'He said "How are you?"'; The double quotes inside the string are not the same as the single quotes that define the boundaries of the string. - 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
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.









