Wednesday, December 20, 2023

Day 14 || let and const in JavaScript || var vs let vs const in JavaScript || Template literals in JavaScript

  JavaScript Topics

Day 14 :

  • let and const
  • Template literals

let and const:

//Declaration
// 1. Redeclaration
// 2.Reassignment
// 3.scope
// 4.Hoisting
var varSibling = "I might cause neglect and no bounderies";
var varSibling = "I am causing neglect (re-declared)";
varSibling = "I am causing neglect second time (re-assigned/updated)";
console.log(varSibling);

let letSibling = "I can commit to change and having boundaries";
letSibling = "I am re-assigning new value";
let letSibling = "My commitment is one time declaration";
console.log(letSibling);

const constSibling = "I remain constant";
constSibling = "I want to change, but I cannot"
const constSibling = "I can't re-declare";
console.log(constSibling);

if (true) {
  var varScope = "I am var and global-scoped ";
  let letScope = "I am let and block-scoped";
  const constScope = "I am const and block-scoped";
  console.log(letScope);
  console.log(constScope)
}
console.log(varScope);
console.log(letScope);
console.log(constScope)
console.log(hoistedVar);
var hoistedVar = "I am hoisted var means declared, but not assigned value";


console.log(hoistedLet)
let hoistedLet = "I am hoisted let means not initialized";

console.log(hoistedConst)
const hoistedConst = "I am hoisted const means not initialized";

Template literals:

let name = "Hema";
let age = 21;
let info = "Student name is:" + name + " and " + age;
let infoTemp = `Student name is: ${name} and ${age}`;
console.log(info);
console.log(infoTemp)


Interview Questions: 

1. What is the difference between var, let, and const when declaring variables?
2. How does let differ from var in terms of block scoping?
3. What are template literals, and how do they differ from traditional string concatenation?

No comments:

Post a Comment

Top 10 | JavaScript Coding Interview Question | Beginner Level

               JavaScript Coding Interview  Q. No. 01/10:  console . log ( "1" + "4" + "1" ) // 141 console . ...