Conversation

Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0

Sharpen your coding skills—try JavaScript challenges on TOOLX now!

advertisement

Data Types and Methods in JavaScript

javascript data tpes

Data Types

A Web page designer can store different types of values such as numbers, characters, or strings in variables. However, the Web page designer must know what kind of data a particular variable is expected to store. To identify the type of data that can be stored in a variable, JavaScript provides different data types.

A Web page designer is not required to specify the data type while declaring variables. Due to this, JavaScript is referred to as a loosely typed language. This means that a variable holding a number can also hold a string value later. The values of variables are automatically mapped to their data types when the script is executed in the browser.

Data types in JavaScript are classified into two broad categories, namely primitive and composite data types. Primitive data types contain only a single value, whereas composite data types contain a group of values.


1. Primitive Data Types

A primitive data type contains a single literal value such as a number or a string. A literal is a static value that you can assign to variables.

Lists the primitive data types.

  • Boolean: Contains only two values namely, true or false

  • Null: Contains only one value namely, null. A variable of this value specifies that the variable has no value. This null value is a keyword and it is not the same as the value zero.

  • Number: Contains positive and negative numbers and numbers with decimal points. Some of the valid examples include 6, 7.5, -8, 7.5e-3, and so on

  • String: Contains alphanumeric characters in single or double quotation marks. The single quote is used to represent a string, which itself consists of quotation marks. A set of quotes without any characters within it is known as the null string


Composite Data Types

A composite data type stores a collection of multiple related values, unlike primitive data types. In JavaScript, all composite data types are treated as objects. A composite data type can be either predefined or user-defined in JavaScript.

Lists composite data types.

  • Objects: Refer to a collection of properties and functions. Properties specify the characteristics and functions that determine the behavior of a JavaScript object

  • Functions: Refers to a collection of statements, which are instructions to achieve a specific task

  • Arrays: Refer to a collection of values stored in adjacent memory locations


Methods

Javascript allows you to display information using the methods of the document object. The document object is a predefined object in JavaScript, which represents an HTML page and allows managing the page dynamically. Each object in JavaScript consists of methods, which fulfill a specific task. There are methods of the document object, which display any type of data in the browser. These methods are as follows:

  • Write(): Displays any type of data.
  • Writeln(): Displays any type of data and appends a new line character.

The following syntax demonstrates the use of the document.write() method, which allows you to display information on the displayed HTML page:

Syntax:

document.write("<data> + variables");

Where,

  • Data: Specified strings enclosed in double quotes.
  • Variables: Specify variable names whose values should be displayed on the HTML page.


The following syntax demonstrates the use of the document.write() method, which appends a new line character:

Syntax:

document.writein("<data>" + variables);


The use of the write() method

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Language</title>
<script>
document.write("JavaScript:");
document.writeln("is a scripting");
document.write("and a case-sensitive language.");
</script>
</head>
<p>
JavaScript: is a scripting and case-sensitive language.
</p>
</html>

The code uses the writeln() method to display the text after the colon without leaving a space. It finally appends a new line character after the text. Then, the text within the write () method is displayed on the same line after leaving a space.

The same paragraph is displayed in the body of the HTML page. Note that the text in the element appears on different lines. In HTML, the text on the second line and a case-sensitive language will not be displayed on the new line in the browser even though the ENTER key is pressed while writing the code. Rather, it will be displayed on the same line with a space. The writeln() method also follows the same format.


JavaScript Data Types Methods javascript data types javascript methods data types in javascript methods in javascript primitive Data types types of data types in javascript composite data types

advertisement