@yagyar001

Почему вылазит ошибка «Uncaught ReferenceError: monthInZero is not defined»?

Всем привет. Учу js. Ситуация вводит в ступор. Есть отрезок кода:
let date = new Date();


if (date.getMonth() <= 9) {
	let monthInZero = '0' + date.getMonth();
} else {
	let monthInZero = date.getMonth();
}

let dateNow = String(date.getFullYear()) + '-' + monthInZero + '-' + date.getDate();

console.log(String(date.getMonth()))

Условие date.getMonth() <= 9 срабатывает, проверял через консоль... Но ругается на переменную monthInZero. Что с ней не так?
  • Вопрос задан
  • 191 просмотр
Решения вопроса 1
E1ON
@E1ON Куратор тега JavaScript
Programming, Gamedev, VR
let date = new Date();

const monthInZero = date.getMonth() <= 9 ? '0' + date.getMonth() : date.getMonth();
let dateNow = String(date.getFullYear()) + '-' + monthInZero + '-' + date.getDate();

console.log(String(date.getMonth()))
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы