JavaScript JavaScript and the DOM Making Changes to the DOM Getting and Setting Text with textContent and innerHTML

Dave Harrison
Dave Harrison
Front End Web Development Techdegree Student 7,400 Points

Use of Const & Let

I am not sure I have got my head completely around the differences between Const and Let and when to use them. In the example in this video, why did Const get used in the text.Content example and Let in the inner.HTML example?

2 Answers

Brett Mchatton
Brett Mchatton
10,279 Points

Use let for a variable that will be changing (array, numbers, inputs values). Then use const for variables that remain unchanged. (a predefined static number, a static name, some elements of the dom). The real difference between them is once you assign "bind" a value to a const you can't change it. When you use let you can reassign the variable with a new value.

In the video, Guil is using const for dom elements that stay static. Then uses let for dom elements that do not. The length of the <ul> is growing and shrinking so the value is constantly changing. The <p> is only a container so the value does not change only the content.

Kasra Tabrizi
PRO
Kasra Tabrizi
Pro Student 6,739 Points

Can we also use "var" instead of "let"?