---
title: "JS 变量应先声明，再使用"
author: "tdp100"
date: "2013-05-28T11:34:05Z"
tags: []
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2013-05-28T11:34:05Z"
id: 1878736
url: "https://tdp100.iteye.com/blog/1878736"
---

1、未先声明，就使用变量会抛出异常
if(aaaa === undefined)
{}

//异常： aaaa is not undefined

2、声明可以在上下文任意位置，但取值是在具体赋值时生效
if(aabc1 === undefined)
{
  console.log("aabc if = ", aabc1)
}
else
{
  console.log("aab else = ",aabc1);
}
var aabc1 = 1;

//打印出aabc if = undefined