-5

I have written javascript code which shows current date, it works wery well. The problem is that, i want to change "var" variable to "let" or "const", but when i change it, itdoesnot work. can you help me? i want modern javascript code.

here is my code




  const objToday = new Date(),
  weekday = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'),
  dayOfWeek = weekday[objToday.getDay()],
  domEnder = function() { let a = objToday; if (/1/.test(parseInt((a + "").charAt(0)))) return "th"; a = parseInt((a + "").charAt(1)); return 1 == a ? "st" : 2 == a ? "nd" : 3 == a ? "rd" : "th" }(),
  dayOfMonth = today + ( objToday.getDate() < 10) ? '0' + objToday.getDate() + domEnder : objToday.getDate() + domEnder,
  months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'),
  curMonth = months[objToday.getMonth()],
  curYear = objToday.getFullYear(),
  curHour = objToday.getHours() > 12 ? objToday.getHours() - 12 : (objToday.getHours() < 10 ? "0" + objToday.getHours() : objToday.getHours()),
  curMinute = objToday.getMinutes() < 10 ? "0" + objToday.getMinutes() : objToday.getMinutes(),
  curSeconds = objToday.getSeconds() < 10 ? "0" + objToday.getSeconds() : objToday.getSeconds(),
  curMeridiem = objToday.getHours() > 12 ? "PM" : "AM";
  yesterday = (function(d){ d.setDate(d.getDate()-1); return d})(new Date);

  var today =  dayOfWeek;
  var todaysDate = dayOfMonth + " " +curMonth + "," +curYear;
  var currentTime = curHour + ":" + curMinute + curMeridiem;
  var currenTDate = "today is " + dayOfWeek;
  var curMonthDay = curMonth + dayOfMonth;
  var yestDate = "Yesterday was: " + yesterday;


  document.querySelector(".h11").textContent = today;
  document.querySelector(".h12").textContent = todaysDate;
  document.querySelector(".hour").textContent = currentTime;
  document.querySelector(".h40").textContent = currenTDate;
  document.querySelector(".h41").textContent = curMonthDay;
  document.querySelector(".h42").textContent = yestDate;


1 Answers1

1

When you use var, the variable declaration is hoisted and initialized with undefined. When you use const or let, they are hoisted but are not initialized. So when you say line 5 is the problem here, you can see why today is throwing an error. Here's a little demo. You'll notice how the var example gives undefined while the const example gives (index):40 Uncaught ReferenceError: Cannot access 'const_name' before initialization.

// With var
var test1 = d1;
console.log(test1);

var d1 =  new Date();


// With const
const test2 = d2;
console.log(test2)

const d2 = new Date();

To fix this you can move const d2 = new Date() above const test2 = d2.

// With var
var test1 = d1;
console.log(test1);

var d1 =  new Date();


// With const
const d2 = new Date();
const test2 = d2;
console.log(test2)

For more reading you can check out: Are variables declared with let or const not hoisted in ES6?

justDan
  • 2,302
  • 5
  • 20
  • 29