GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.runoob.com — Node.js REPL(交互式解释器) | 菜鸟教程
Node.js 教程
Node.js 教程
Node.js 概念
Node.js 安装配置
Node.js VS Code
Node.js NVM
Node.js 创建第一个应用
Node.js 工作机制
NPM 使用介绍
PNPM 使用介绍
Node.js REPL
Node.js 异步编程
Node.js 回调函数
Node.js Promise
Node.js async/await
Node.js 事件循环
Node.js EventEmitter
Node.js Buffer
Node.js Stream
Node.js 模块系统
Node.js 模块导入
Node.js 函数
Node.js 路由
Node.js 全局对象
Node.js 常用工具
Node.js 文件系统
Node.js 内置模块
Node.js 测验
Node.js 应用Node.js 构建简单应用 Node.js GET/POST请求 Node.js util 模块 Node.js Web 模块 Node.js Express 框架 Node.js RESTful API Node.js 多进程 Node.js JXcore 打包 Node.js MySQL Node.js MongoDB npx 入门教程Node.js REPL(交互式解释器)Node.js 提供了一个内置的 REPL(Read-Eval-Print Loop),这是一个交互式编程环境,可以在终端中运行 JavaScript 代码。 REPL 的名称来源于它的主要操作:读取(Read)、执行(Eval)、打印(Print)和循环(Loop)。 Node 自带了交互式解释器,可以执行以下任务:
REPL 可以直接输入并立即执行 JavaScript 代码,快速验证代码片段。 REPL 适合测试简单逻辑和调试以及尝试新语法或 Node.js API。 开始学习 REPL 我们可以输入以下命令来启动 Node 的终端: node 执行后出现如下内容: # node Welcome to Node.js v20.1.0. Type ".help" for more information. > 这时我们就可以在 > 后输入简单的表达式,并按下回车键来计算结果。 简单的表达式运算接下来让我们在 Node.js REPL 的命令行窗口中执行简单的数学运算: $ node > 1 +4 5 > 5 / 2 2.5 > 3 * 6 18 > 4 - 1 3 > 1 + ( 2 * 3 ) - 4 3 > 使用变量你可以将数据存储在变量中,并在你需要的时候使用它。 变量声明需要使用 var 关键字,如果没有使用 var 关键字变量会直接打印出来。 使用 var 关键字的变量可以使用 console.log() 来输出变量。 $ node
> x = 10
10
> var y = 10
undefined
> x + y
20
> console.log("Hello World")
Hello World
undefined
> console.log("www.runoob.com")
www.runoob.com
undefined
多行表达式Node REPL 支持输入多行表达式,这就有点类似 JavaScript。接下来让我们来执行一个 do-while 循环: $ node
> var x = 0
undefined
> do {
... x++;
... console.log("x: " + x);
... } while ( x < 5 );
x: 1
x: 2
x: 3
x: 4
x: 5
undefined
>
... 三个点的符号是系统自动生成的,你回车换行后即可。Node 会自动检测是否为连续的表达式。 下划线(_)变量你可以使用下划线(_)获取上一个表达式的运算结果: $ node > var x = 10 undefined > var y = 20 undefined > x + y 30 > var sum = _ undefined > console.log(sum) 30 undefined > REPL 命令Node.js REPL 的常用命令和快捷键汇总表:
停止 REPL前面我们已经提到按下两次 ctrl + c 键就能退出 REPL: $ node > (^C again to quit) > REPL 进阶功能
Gif 实例演示接下来我们通过 Gif 图为大家演示实例操作:
AI 思考中...
分类导航
Advertisement
| ||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |