Converting Javascript Objects into Strings with JSON.stringify()

JSON stringification is the process of converting a Javascript object to a flat JSON string that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON.stringify(), as the Javascript standard specifies.

JSON.stringify() is the opposite of JSON.parse(), which converts JSON into Javascript objects. This article and Udacity’s JSON.parse() article have similar structures to emphasize the close relationship between the two functions.

Continue Reading

Manipulating HTML Through the Javascript Document Object Model (DOM)

Javascript creates dynamic content on web pages by manipulating the underlying HTML structure. Browsers manage HTML internally as a tree of objects called the Document Object Model, or DOM. Developers refer to the tree objects as “elements.” The DOM also applies to XML and XHTML documents, which most modern browsers can display as well.

The DOM standard specifies how a browser builds an HTML tree and how scripting languages like Javascript should access elements in that tree. Although the Javascript standard implements concepts from the DOM standard, the two standards are different, maintained by different groups.

Continue Reading

Comparing Values by Using Operators: Javascript AND, Javascript OR, and Many More

Comparing one value to another is an important programming technique that developers use to create logical flows. Javascript provides many ways to compare simple values using language constructs called “operators.”

The Javascript standard defines two different types of simple operators.

  • Logical Operators — logical relationships between values
  • Comparison Operators — how values are related to each other

These operators work with Javascript data types that hold simple values, like strings or numbers, but cannot handle complex values like Javascript objects, arrays, or functions. To compare complex objects, developers must write their own comparison functions.

Make sure that the compared values or variables are the same data type; otherwise you may get an answer you don’t expect. Each operator type has different rules for valid comparisons, which the next sections will cover in more detail.

Continue Reading

Handling Errors with Javascript Try Catch Statements

To the surprise of many programmers, errors in Javascript programs are not always a mark of bad programming. Sometimes, it’s important for an error to occur so a programmer knows something is wrong, like when a program doesn’t get the input it needs or can’t connect to a resource properly.

By default, a Javascript error prevents the execution of any more Javascript code. In some contexts, though, developers might want to skip over an error and continue executing a program. This process is called error handling, and Javascript manages this with special code structures called Javascript try catch statements, defined in the Javascript standard.

Continue Reading

Creating and Managing Program Output: Javascript Print Strategies

Developers use Javascript to create web content, format web pages, and return values from supporting functions that do calculations. Developers often need a way to track what Javascript code is doing, so they generate Javascript output.

The Javascript standard documents two major techniques for generating output: printing to the developer console and printing into HTML on a web page.

Continue Reading

How to Manage Multiple Values with Javascript Arrays

A Javascript array is a type of variable that holds multiple valid Javascript values. There is no limit to array size except for the amount of memory available when creating the array, according to the Javascript standard.

Since Javascript arrays can hold multiple values, programmers can group related information together, from sports scores to grades. Indexing values in an array makes it easy to access related values quickly and to perform calculations without having lots of extra variables.

Javascript arrays help developers keep variables and values organized in an easily accessible way inside a program. In this guide, we’ll walk through how to manage multiple variables and values with Javascript arrays.

Continue Reading