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

8 Key excerpts on "Javascript Reference Data Types"

  • Professional JavaScript for Web Developers
    • Matt Frisbie(Author)
    • 2019(Publication Date)
    • Wrox
      (Publisher)
    5 Basic Reference Types

    WHAT'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
  • Professional JavaScript for Web Developers
    • Nicholas C. Zakas(Author)
    • 2011(Publication Date)
    • Wrox
      (Publisher)
    Chapter 5 Reference Types WHAT’S IN THIS CHAPTER?
    • Working with objects
    • Creating and manipulating arrays
    • Understanding basic JavaScript data types
    • Working with primitives and primitive wrappers
    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.
    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 book.
    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:
    var person = new Object();
    This code creates a new instance of the Object reference type and stores it in the variable person . The constructor being used is Object() , which creates a simple object with only the default properties and methods. ECMAScript provides a number of native reference types, such as Object , to help developers with common computing tasks.
    THE OBJECT TYPE
    Up to this point, most of the reference-value examples have used the Object type, which is one of the most often-used types in ECMAScript. Although instances of Object don’t have much functionality, they are ideally suited to storing and transmitting data around an application.
    There are two ways to explicitly create an instance of Object . The first is to use the new operator with the Object
  • The JavaScript Workshop
    eBook - ePub

    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
  • Confident Web Design
    eBook - ePub

    Confident Web Design

    How to Design and Create Websites and Futureproof Your Career

    • Kenny Wood(Author)
    • 2020(Publication Date)
    • Kogan Page
      (Publisher)
    08

    JavaScript Part 2

    What we will learn in this chapter

    In Part 2 of our JavaScript section, we will be covering data types, strings and, one of the most important and challenging concepts of JavaScript, objects.

    Data types

    So far on our journey into the world of JavaScript, we have created many variables and assigned them different values. We have assigned them some snippets of text, such as in our ‘Hello World!’ examples. We have also assigned them numbers, like in our function examples. Each of these different types of information that we assign to a variable is called a ‘data type’. So far we have seen strings (text, such as ‘Hello World’) and numbers. Let’s quickly see examples of these again to refresh our understanding:
    var x = "Hello"; // Assigning a string var y = 12; // Assigning a number
    Data types play a very important part in programming and understanding them is a fundamental part of your learning of JavaScript and its concepts. JavaScript handles the assigning of data types for you when you declare your variables. It will detect the data type you have assigned, based on your syntax. You do not have to specifically state which type of data your variable holds, like you might do in a different programming language.
    It’s important to understand both the benefits and limitations of each data type, to help you to understand what you can do with each. So let’s first understand just why data types are so important. Consider the following snippet of code:
    var x = "Hello"; // Assigning a string var y = 12; // Assigning a number var i = x + y; console.log(i);
    Now pause for a second and think about this snippet of code. What do you think will happen when we try to run this code? Will it break the code and produce an error message? Or will it try to output something in the console and fail there?
  • Beginning JavaScript
    • Jeremy McPeak(Author)
    • 2015(Publication Date)
    • Wrox
      (Publisher)
    2 Data Types and Variables
    WHAT YOU WILL LEARN IN THIS CHAPTER:                
    • Representing data in code
    • Storing data in memory
    • Making calculations
    • Converting data
    WROX.COM CODE DOWNLOADS FOR THIS CHAPTER
    You 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
  • Quick JavaScript
    eBook - ePub
    Chapter 2 JavaScript The Bare Minimum
    DOI: 10.1201/9781003359609-2
    This 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"
  • Decoding JavaScript
    eBook - ePub

    Decoding JavaScript

    A Simple Guide for the Not-so-Simple JavaScript Concepts, Libraries, Tools, and Frameworks (English Edition)

    Code 1.13: Array datatype
  • Objects : Objects are also a complex data type which can store multiple values. However, unlike arrays, in objects, you can name the data that you are storing. Objects are used for modelling a real-life object. The multiple values are named and stored inside curly brackets.
    1. var employee = {name: 'John Doe', age: 24, city: 'London'}
      Code 1.14: Objects datatype
  • JavaScript automatically detects the type of the value of a variable. Hence, JS is also called as a 'dynamically typed language '.

    Operators in JavaScript

    JavaScript (JS) operators allow you to do mathematical operations, combine two strings, compare values, determine logic between variables or values, and also find the data type of a variable. Since this book caters specifically towards advanced JavaScript, we expect the readers to have a basic idea of JavaScript operators. While we will go through the theory of the basic JS concepts, and also look at one or two examples, we encourage the readers to practices different patterns on their own to refresh their knowledge of these basic concepts.
    There are the following six major types of operators in JavaScript:
    • Arithmetic Operators : They are used to perform arithmetic operations on numbers. Arithmetic operators include + (addition), - (subtraction), * (multiplication), / (division), % (modulus, the remainder of a division), ++ (increment), and – (decrement).
      1. var sum = 4 + 3;      //Output will be 7
        Code 1.15:
  • LPI Web Development Essentials Study Guide
    • Audrey O'Shea(Author)
    • 2023(Publication Date)
    • Sybex
      (Publisher)
    Chapter 14 JavaScript Data
    LINUX PROFESSIONAL INSTITUTE, WEB DEVELOPMENT ESSENTIALS EXAM 030‐100 OBJECTIVES COVERED IN THIS CHAPTER:
    • 034 JavaScript Programming
      • 034.2 JavaScript Data Structures: The Candidate should be able to use variables in JavaScript code. This includes understanding values and data types. Furthermore, the candidate should understand assignment operators and type conversion and be aware of variable scope.
      • Key Knowledge Areas:
        • Define and use variables and constants
        • Understand data types
        • Understand type conversion/coercion
        • Understand arrays and objects
        • Awareness of the variable scope
      • Files, terms, and utilities:
        • =, +, ‐, *, /, %, ‐‐, ++, +=, ‐=, *=, /=
        • var, let, const
        • boolean, number, string, symbol
        • array, object
        • undefined, null, NaN
    In the previous chapter, you learned the rules and format for writing JavaScript code and including it in your HTML pages. In this chapter, you'll learn about the types of data and how to work with variables and their values. We'll also look at the math operators, some shortcuts, and how to create and use arrays and objects to save time and avoid human error. There's a lot to cover, so let's get started!

    Variables, Constants, and Scope

    One of the first things I teach my information technology students is that the four main jobs of a computer system are input, output, processing, and storage. It's what programming and computer systems are all about. High‐level languages like JavaScript make it easier to tell computers how to accept your input, process it, and create output.
    You start with the data that you want to manipulate in some way. Maybe it's sales receipts for a business, actuary tables used to figure out the cost of someone's insurance, or an ingredient list for a recipe that you'll later make in a larger batch. You must input data into a program before the other steps can take place.
    Data is gathered and stored in variables and constants. Later in this chapter you'll learn about advanced data structures used to organize data that's been input. In JavaScript and most other programs, variables and constants are declared differently and have different restrictions on their use. Variables and constants have two similarities—the variable or constant must start with an underscore or a letter, and their values are assigned in name=
  • 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.