Javascript for Test Automation (E3)

Data Types

Mohamed Yaseen
4 min readJul 11, 2022

A core job of most programming languages is dealing with data. Different data types represent different kinds of data. Data types help programmers and their programs determine what they can and cannot do with a given piece of data.

JavaScript has five so-called primitive data types, every type that is not a primitive type is an object type:

  • String
  • Number
  • Undefined
  • Null
  • Boolean

Data type values can be represented by literals. A literal is any notation in source code that allows you to represent a fixed value. In JavaScript, for example, all of the following are literals:

'Hello, world!'     // string literal
3.141528 // numeric literal
true // boolean literal
{ a: 1, b: 2 } // object literal
[ 1, 2, 3 ] // array literal
undefined // undefined literal

Strings

A string is a collection of characters in a certain order. We commonly have to work with text data in programming, such as names, messages, and descriptions. Strings are used in JavaScript to represent such data. String literals are written with either single (‘hi there’) or double (‘hi there”) quotes on either side of the text; the quotes are syntactic components, not part of the value.

> "He said, 'Hi there!'"    // with double quotes
= "He said, 'Hi there!'"
> 'He said, \'Hi there!\'' // with single quotes and escaping
= "He said, 'Hi there!'"
> 'He said, "Hi there!"' // with single quotes
= 'He said, "Hi there!"'
> "He said, \"Hi there!\"" // with double quotes and escaping
= 'He said, "Hi there!"'

The backslash, also known as the escape character (\), informs the computer that the next character is not syntactic but is part of the string. Escaping a quotation character prevents JavaScript from interpreting it as the string’s end. Keep in mind the sort of character you use: a forward slash (/) has no meaning inside a string.

Template literals are a recent JavaScript innovation. They employ backticks (`) to facilitate a string interpolation process. Try the following code in node to see how it works:

> `5 plus 5 equals ${5 + 5}`
= '5 plus 5 equals 10'
/* String interpolation is a handy way to merge JavaScript expressions with strings. The basic syntax is:*/`Blah ${expression} blah.`

Numbers

In JavaScript, the Number data type represents all types of numbers. Distinct data types exist in certain programming languages for different number kinds, such as integers, floating-point numbers, and fixed-point (decimal) numbers. Number is the only data type in JavaScript that represents all forms of numbers. We’re talking about real numbers for you math whizzes out there.

1, 2, -3, 4.5, -6.77, 234891234 // Examples of numeric literals/* Note that you can't use commas or periods for grouping: neither 123,456,789 nor 123.456.789 is valid. You must use 123456789 instead. Similarly, you can't use European-style period separators with a comma before the decimal part: use 543.21, not 543,21.*/

Booleans

Boolean values reflect whether something is “on” or “off.” For example, boolean values may be used to represent the status of a light switch in your application. True and false are the boolean literal values:

> let toggleOn = true
= undefined
> let sessionActive = false
= undefined
/*When working with comparison operators, Boolean values take center stage. In the next part, we will discuss comparisons. For the time being, it is sufficient to understand that comparisons determine if a value is equal to, less than, or higher than another value. They yield a boolean result (true or false).*/> 5 === 5
= true
> 100 < 99
= false

Undefined

We need a means to describe the lack of a value in programming. This is done in JavaScript with the value undefined. Undefined is the value of a variable when it is not defined. Undefined can be thought of as the absence of a value. We may also use the literal undefined explicitly.

> let foo
= undefined
> foo
= undefined
> let bar = 3
= undefined
> bar
= 3
/* As you can see, we declare the foo variable without giving it a value. Using that variable returns undefined. On the other hand, we declare bar with an initial value of 3. Thus, using bar in an expression returns 3.*/

Null

Null, like undefined, signifies the deliberate lack of a value. Null frequently denotes nothingness or nothing. The main distinction between null and undefined is that null must be used intentionally, whereas undefined might occur implicitly. Because these two values are so similar in their use and behavior, some individuals believe that include both in JavaScript is a mistake. We’ll look into null a little later. For the time being, consider it a value that symbolizes emptiness or nothing.

> let foo = null

The typeof Operator

Every value in your JavaScript scripts is assigned a data type. The typeof operator can be used to determine the type of a certain value. typeof returns a string containing the value of its operand’s type. As an example:

> typeof 1
= 'number'
> typeof 'foo'
= 'string'
> typeof true
= 'boolean'
> typeof undefined
= 'undefined'
> typeof null
= 'object'
> typeof [1, 2, 3]
= 'object' //JavaScript, arrays are objects.

--

--

Mohamed Yaseen

Experienced QA Automation Lead with expertise in test automation, frameworks, and tools. Ensures quality software with attention to detail and analytical skills