Share via


JavaScript Console commands

Visual Studio provides commands that you can use in the JavaScript Console window to send messages and perform other tasks. For info on using the JavaScript Console, see Quickstart: Debugging apps (JavaScript). The information in this topic applies to Windows Store apps built using JavaScript.

If the JavaScript Console window is closed, you can open it while you're debugging in Visual Studio by clicking Debug > Windows > JavaScript Console.

console object commands

This table shows the syntax for the console object commands that you can use in the JavaScript Console window. You use the console object to send a message to the console from your code. This object provides a number of forms so that you can distinguish between informational messages and error messages, if you want to.

Note

You can use the longer command form window.console.[command] if you need to avoid possible confusion with local objects named console.

Command

Description

Example

assert(expression, message)

Sends a message if expression evaluates to false.

console.assert((x == 1), "assert message: x != 1");

clear()

Clears messages from the console window, including script-error messages, and also clears script that appears in the console window. Does not clear script that you entered into the console input prompt.

console.clear();

dir(object)

Sends the specified object to the console window and displays it in an object visualizer. You can use the visualizer to inspect properties in the console window.

console.dir(obj);

error(message)

Sends message to the console window. The message text is red, and it's prefaced by an error symbol.

console.error("error message");

Objects passed using the command will be converted to a string value.

info(message)

Sends message to the console window. The message is prefaced by an information symbol.

console.info("info message");

log(message)

Sends message to the console window.

console.log("logging message");

Objects passed using the command will be converted to a string value.

msIsIndependentlyComposed(element)

Used in Web apps. Not supported in Windows Store apps built for Windows using JavaScript.

Not supported.

profile(reportName)

Used in Web apps. Not supported in Windows Store apps built for Windows using JavaScript.

Not supported.

profileEnd()

Used in Web apps. Not supported in Windows Store apps built for Windows using JavaScript.

Not supported.

warn(message)

Sends message to the console window, prefaced by a warning symbol.

console.warn("warning message");

Objects passed using the command will be converted to a string value.

Miscellaneous commands

These commands are also available in the JavaScript Console window.

Command

Description

Example

$0, $1, $2, $3, $4

Returns the specified element to the console window. $0 returns the element currently selected in DOM Explorer, $1 returns the element previously selected in DOM Explorer, and so on, up to the fourth previously selected element.

$3

$(id)

Returns an element by ID. This is a shortcut command for document.getElementById(id), where id is a string that represents the element ID.

$("contentHost")

$$(div)

Returns an element using CSS selector syntax. This is a shortcut command for document.querySelector(div), where div is a string that represents a DIV element class name.

$$(".itemlist")

cd()

cd(window)

Enables you to change the context for expression evaluation from the default top-level window of the page to the window of the specified frame. Calling cd() without parameters returns the context to the top-level window.

cd();

cd(myframe);

select(element)

Selects the specified element in DOM Explorer.

select(document.getElementById("element"));

select($("element"));

select($1);

dir(object)

Returns a visualizer for the specified object. You can use the visualizer to inspect properties in the console window.

dir(obj);

Checking whether a console command exists

You can check whether a specific command exists before attempting to use it. This example checks for the existence of the console.log command. If console.log exists, the code calls it.

    if (console && console.log) {
        console.log("msg");
    }

Examining objects in the JavaScript Console window

You can interact with any object that's in scope when you use the JavaScript Console window. To inspect an out-of-scope object in the console window, use the console.dir command from your code, or set a breakpoint by clicking Breakpoint > Insert Breakpoint.

See Also

Concepts

Quickstart: Debugging apps (JavaScript)