> For the complete documentation index, see [llms.txt](https://yyaozhibo.gitbook.io/qian-duan-xue-xi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yyaozhibo.gitbook.io/qian-duan-xue-xi/js/es6-gang-xu/han-shu-de-kuo-zhan/han-shu-can-shu-de-mo-ren-zhi.md).

# 函数参数的默认值

1\.

```javascript
function log(x, y = 'world') {
  console.log(x, y)
}

```

这里计算的是是否有值传入 y，如果没有（值为 `undefined`）则设置 y 的默认值为 `world`

2\.

```javascript
function Point(x = 0, y = 0) {
  this.x = x
  this.y = y
}

const p = new Point()

console.log(p)
//Point { x: 0, y: 0 }

```

3\.

```javascript
let x = 99

function foo(p = x + 1) {
  console.log(p)
}

foo()
// 100

x = 100

foo()
// 101

```
