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

3 Key excerpts on "Javascript Primitive Data Types"

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

    Confident Web Design

    Master the Fundamentals of Website Creation and Supercharge Your Career

    • Kenny Wood(Author)
    • 2018(Publication Date)
    • Kogan Page
      (Publisher)

    ...Each value, or piece of data, has a type. That type defines the type of content that we are expecting. There are six different basic types for values. They are: numbers; strings; Booleans; objects; functions; and undefined values. Numbers As you might have guessed, a value with a ‘number’ type is a value formed of a numerical value. They are written and displayed as you might expect, as numbers, such as ‘4231432’. Strings Strings are simply snippets of text. It could be a paragraph, a sentence, or even a single character. It is simply a snippet of text of any length. Boolean A Boolean is a true/false value. It is usually used for evaluation of a statement. For example, 3<2 would render false, while 3>2 would render true as 3 is a larger number than 2. Objects, functions, undefined These will be covered in more detail in the subsequent chapters. Variables Variables are what we use to store our data values. They are like buckets of information where we can store and modify the contents as we please. In JavaScript we use the keyword ‘var’ followed by a keyword and an equals sign to declare a variable, like so: <script> var x = 100; var helloWorld = &#x201C;Hello World!; </script> In this example, the x variable will contain the number 100, while the helloWorld variable will contain the text ‘Hello World!’. Naming variables When naming things with JavaScript, such as variables, just like with HTML ids and classes, we can’t have any spaces in the word. However, when working with programming languages, it is common practice to (instead of using hyphens) replace spaced words with a style of writing known as camelCase. The basic principle of camelCase, is that for each new word, we remove the space and replace the first letter of the new word with a capital letter, except the very first letter of the phrase, which is always lowercase. For example, the phrase ‘My New Variable’ would become ‘myNewVariable’...

  • HTML, CSS & JavaScript in easy steps

    ...16 Perform Useful Operations This chapter describes the JavaScript operators and demonstrates how they can be used in your scripts. Convert Values Do Arithmetic Assign Values Make Comparisons Assess Logic Examine Conditions Juggle Bits Force Order Summary Convert Values Before performing operations in JavaScript it is important to recognize the data types of the values you are working with in order to avoid unexpected results. For example, the value 42 is a number, but the value ‘42’ is a string, so attempting to perform an addition with ‘42’ + 8 will return a string result of ‘428’, not the number 50. Happily, JavaScript provides a number of ways to return versions of values in other data types – without changing the value’s original data type. Strings to Numbers The JavaScript parseInt() built-in function can return an integer whole number version, in the number data type, of a string specified within its parentheses. For example, parseInt(‘42’) will return the number 42, so 42 + 8 will return a number result of 50. Similarly, the JavaScript parseFloat() built-in function can return a floating-point number version, in the number data type, of a string specified within its parentheses. Both these methods allow alphabetic characters to follow the numeric part of the specified string and strip them from the result – for example, parseInt(‘42nd Street’) returns number 42. If either of these functions cannot find a numeric value at the beginning of the specified string, the result will be NaN – a JavaScript property meaning “Not a Number”. You can also check if a value is not a number by specifying the value within the parentheses of a JavaScript isNaN() built-in function. This, too, will first attempt to find a number at the beginning of the specified value and return false if it finds a number (even from a specified string), otherwise it will return true if it cannot find a number. Conversion of data types is known as “coercion”, and it can be explicit or implicit...

  • Generative Design
    eBook - ePub

    Generative Design

    Visualize, Program, and Create with JavaScript in p5.js

    • Benedikt Gross, Hartmut Bohnacker, Julia Laub, Claudius Lazzeroni, Marie Frohling(Authors)
    • 2018(Publication Date)

    ...All graphic commands thereafter refer to this altered coordinate system. 12 translate (40, 20); rotate (0.5); scale (1.5); In this example, the coordinate system is moved 40 pixels to the right and 20 pixels downward, then rotated 0.5 radians (about 30°), and finally scaled by a factor of 1.5. In Processing, angles are generally shown in radians, in which 180° equals the number pi (≈ 3.14) and the rotation is in a clockwise direction. 12 Variables and Data Types In a program, information is stored in variables so that it is available to other parts of the program. These variables can have any name other than the keywords for JavaScript keywords. Keywords can be recognized by their special color in the editor. 13 var myVariable; myVariable = 5; 13 The variable myVariable is created. The value 5 is then saved there. Variables can contain different data types. Unlike most other programming languages, JavaScript does not have to take this into account when creating a variable. Nevertheless, it is important to keep track of which variables store which type of value. For example, data types can be: 14 var myBooleanvalue = true : 15 var myInteger = 7: 16 var myFloatingPointNumber = -3.219: 17 var myCharacter = "A": 18 var myString = "This is a text": 14 Logic values (Boolean values): true or false 15 Integers: e.g., 50, -532. 16 Floating point value: e.g., 0.02, -73.1. 17 A single character: e.g., “a”, “A”, “9”, “&”. 18 Character string/text: e.g., “Hello, world”. Arrays When working with a large number of values, it is inconvenient to have to create a variable for each value. An array allows a list of values to be managed. 19 var planets = ["Mercury", "Venus", "Earth", "Mars". "Jupiter", "Saturn", "Uranus", "Neptune"]: 19 Arrays are defined by their square brackets...