- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL 條件查詢
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線查詢
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 限制結果
- Node.js - MongoDB 連線查詢
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
NodeJS - console.log() 方法
Node.js 的 `console.log()` 方法用於將給定訊息列印到標準輸出流(stdout),即終端或其他日誌記錄機制,例如檔案或網路連線。它接受任意數量的引數,並以空格分隔列印它們,並在末尾新增換行符。
假設我們需要在控制檯上列印一些重要訊息,Node.js 的 `console.log()` 方法很有用。如果我們想列印任何函式的輸出等,`console.log()` 方法也可以完成這項任務。為了更好地理解,讓我們瞭解一下 Node.js 的 `console.log()` 方法的語法和用法。
語法
以下是 Node.js `console.log()` 方法的語法:
console.log([data] [, …args]);
引數
此函式接受兩個引數,如下所示。
data − 此引數指定將在控制檯上顯示的訊息。
args − 這是一個可選引數,它儲存將傳遞給 data 的替換值。
返回值
此方法不返回任何內容;相反,它將格式化的訊息列印到控制檯上的 stdout 中的新行。
示例
Node.js 的 `console.log()` 方法接受一個引數(`data`)。
在這個例子中,我們只使用 `data` 引數呼叫 `console.log()` 方法。
console.log("Welcome to Tutorialspoint");
console.log("Simply Easy Learning at your fingertips");
輸出
正如我們在輸出中看到的,`console.log()` 方法列印了我們作為 `data` 傳遞的訊息。
Welcome to Tutorialspoint Simply Easy Learning at your fingertips
示例
Node.js 的 `console.info()` 方法將接受一個可選引數(`args`)。
在這個例子中,我們使用兩個引數 `data` 和 `args` 呼叫 `console.log()` 函式。我們傳遞 `string` 值作為替換值。
console.log("%s to %s", "Welcome", "Tutorialspoint");
console.log("%s Easy Learning at your %s", "Simply", "fingertips");
輸出
正如我們在輸出中看到的,我們傳遞到 `data` 中的訊息與我們作為 `args` 傳遞的替換值一起列印到 `stdout` 的新行。
Welcome to Tutorialspoint Simply Easy Learning at your fingertips
示例
在下面的例子中,我們做的與上面的例子類似,但是我們傳遞的是 `integer` 值作為 `args`,而不是 `string` 值。
console.log("Bahubali - %d, Bahubali - %d", 1,2);
console.log("Bahubali - %d collected %d crores around the globe", 2, 2000);
輸出
正如我們在輸出中看到的,替換值被傳遞到 `data` 中,並列印到 `stdout` 的新行。
Bahubali - 1, Bahubali - 2 Bahubali - 2 collected 2000 crores around the globe
