Javascript 为什么我的Node.js if else条件无法识别?
我的代码 当我跑的时候Javascript 为什么我的Node.js if else条件无法识别?,javascript,node.js,Javascript,Node.js,我的代码 当我跑的时候 console.log('Starting app'); const fs = require('fs'); const os = require('os'); const _ = require('lodash'); const notes = require('./notes.js'); var command = process.argv[2]; console.log(command); if (Comment = 'add') { consol
console.log('Starting app');
const fs = require('fs');
const os = require('os');
const _ = require('lodash');
const notes = require('./notes.js');
var command = process.argv[2];
console.log(command);
if (Comment = 'add') {
console.log('adding new note');
} else if (command = 'list') {
console.log('listing all notes');
} else {
console.log('command not recognized');
}
我得到
else if语句有什么问题?
=
是赋值运算符。您正在将
赋值给Comment
,然后检查该值在布尔上下文中的计算结果是否为true
,当然它会这样做。您应该使用==
运算符:
if(注释=='add'){
//这里-^
另外,
Comment
未在任何地方声明,这将导致ReferenceError
并导致进程崩溃。因回答明显重复而被否决。Comment='add'
不是比较,而是赋值。其值是赋值后的Comment
的值(即'add'
)这从来都不是错的。请阅读if(Comment='add')
->if(command='add')
Node.js不涉及此问题。好的,它来自Node.js手册,但是javascipt语法。
node app.js list
Starting app
Starting node.js
list
adding new note