Computer Science

Javascript Type Conversion

Javascript type conversion refers to the process of automatically or explicitly converting data from one data type to another in Javascript. This is important for ensuring that operations and comparisons between different data types can be performed accurately. Type conversion can occur implicitly, such as when a string is concatenated with a number, or explicitly using built-in functions like parseInt() or Number().

Written by Perlego with AI-assistance

3 Key excerpts on "Javascript Type Conversion"

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.
  • 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. Where ‘42’ + 8 returns the string ‘428’ this is implicit coercion. Where String( 42 ) returns the string ‘42’ this is explicit coercion.
  • 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)
    What do you expect to see now that we know how JavaScript evaluates expressions? If you’ve been paying attention, you will have guessed that the output would be ‘Hello World146’ and you would be absolutely right. Let’s step through it again. JavaScript sees the ‘Hello World’ and decides it is a string, then it sees the number 14, decides it can’t add a string and a number together, so it must join them instead, at which point we have the string ‘Hello World14’; then it encounters the final part of the expression, 6, at which point JavaScript understands that it can’t add together the string ‘Hello World14’ with 6, so it must also join them together. Resulting in an output of ‘Hello World 146’.
    In JavaScript data types are dynamic. This means that we can swap out variables to contain different data types at will. If we have a variable that contains a number, we can very easily convert that to a string and vice versa. This is incredibly useful because it means we are never restricted to what we can assign to our variables, even if they have already been declared. The following example highlights this benefit.
    Passage contains an image
    var x; // variable x is undefined x = “Hello World”; // variable x is now a string x = 12; // variable x is now a number x = x + “Hello World”; // variable x is now a string again
    We can pass around variables and reassign them at will to any data type we so choose, without ever having to actually specify which data type we are using. This is a huge benefit to the way JavaScript handles data types.
    Knowing the data type of your variable is very important, as each data type allows us to perform different, data type-specific operations on the variable. Let’s look at some examples of how different data types allow us to perform different actions.

    Numbers

    Variables of the ‘number’ data type are declared by setting the variable to an integer without any quotes around it, like so:
    Passage contains an image
    var x = 12; // Data type is a number
    The lack of quotes is extremely important; it’s how we tell JavaScript to treat the variable as a number and not a string. Simply wrapping quotes around a number, like so:
    Passage contains an image
    var x = “12”; // Data type is a string
    We will render the number as a string, and it will therefore be treated as text, which means we lose the actions we can perform on a variable with a data type of number. Declaring numbers is very easy; we can specify any number we like of any size. We can even use decimal points like so:
    Passage contains an image
    var x = 12.55;
    The main benefit of ensuring our variable is of the number data type, is that we can perform arithmetic operations on the variable as we have seen before. Let’s quickly recap with an example of performing some basic arithmetic on a variable:
  • Designing and Developing Robust Instructional Apps
    • Kenneth J. Luterbach(Author)
    • 2018(Publication Date)
    • Routledge
      (Publisher)
    Technically, as discussed in Section 3.1.4, such commands are calls to subroutines, which many programmers also refer to as functions or methods. In the Spider-Monkey version of JavaScript, we use print for output and readline for input. Other programming languages are similar in this regard. For example, the Pascal programming language uses write for output and readln for input; Perl uses print for output and for input; BASIC and Python use print for output and input for input. To summarize this section, we have written interactive programs in JavaScript to obtain the user’s name and the user’s response to a question. Those programs have revealed that computer programs contain variables and statements. The fundamental building blocks of computer programs are variables, statements, and data structures. Once you know those fundamental building blocks, you can combine them in unique ways to express yourself creatively and to engage in problem solving. Note that variables contain information, which we generally refer to as data. Each variable contains one particular type of data. With knowledge of three data types and the skill to use three types of statements, three data structures, and one technique, you will be able to program a computer for general purpose problem solving and creative expression. 3.1.2 Three Data Types In JavaScript programming, a variable can be a number, a character string, or a Boolean value. A Boolean value is either true or false. The Boolean type is named after George Boole, an early nineteenth-century mathematician and philosopher, who developed Boolean algebra or Boolean logic. As an aside, Boolean algebra offers a set of 16 propositions based on two variables (x and y), each of which is either true or false