JavaScript Day 23 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation
100 Days of JavaScript Interview Prep: A Beginner’s Journey


Welcome to Day 23–100 Days of JavaScript Interview Prep: A Beginner’s Journey
Embarking on a 100-day adventure focused on learning, practice, and growth sounds like an incredibly rewarding journey.
DAY 23 (April 14th, 2024) — ES6 Classes Part 5 - Interview Questions ?
Note — Most important interview question
Question 1— How to create ES6 Class with private field and private method ?
Answer :
ES6 introduced private field and private field can be declared with help of # pre-fix.
Note : As the name specify, private can access only within the class itself who declared or define it. It means can’t access or modified from outside the class
Let’s now understand it with basic example
class MyCard {
#address; // Private property
constructor(address) {
this.#address = address; // Assigning a value to the private property
}
// Public method to get the address
getAddress() {
return this.#address;
}
// Public method to set the address
setAddress(address) {
this.#address = address;
}
}
const card = new MyCard('123 road street');
console.log(card.getAddress()); // Output: 123 road street'
// Using the public method to set the address
card.setAddress('new roaod 1223');
console.log(card.getAddress());
// Output: new roaod 1223
b) First Error Use Case
Trying to access the private property directly will result in an error
class MyCard {
#address; // Private property
constructor(address) {
this.#address = address;
// Assigning a value to the private property
}
// Public method to get the address
getAddress() {
return this.#address;
}
// Public method to set the address
setAddress(address) {
this.#address = address;
}
}
const card = new MyCard('123 road street');
// Trying to access the private property directly will result in an error
console.log(card.#address);
// it will give error in terminal

c) Second Error, Use Case
Trying to set the private property directly will also result in an error
class MyCard {
#address; // Private property
constructor(address) {
this.#address = address;
// Assigning a value to the private property
}
// Public method to get the address
getAddress() {
return this.#address;
}
// Public method to set the address
setAddress(address) {
this.#address = address;
}
}
const card = new MyCard('123 road street');
// Trying to set the private property directly will also result in an error
card.#address = 'new road 1234 street';
// it will give error in terminal
//SyntaxError: Private field '#address' must be declared in an enclosing class

Question 2 — What do you understand by Static Private ES6 method and field? Please explain with example
Answer —
ES6 introduced Static Private, and it is similar to normal Static class with advantage of Private.
Let’s see how it works with simple example :)
As we also aware now that private always start with # and if we need to create Static — we need to add prefix Static
Which means when we need static + Private == static #fieldname or static #methodname()
To learn more about Static Or private basic foundation, please follow our below Day 20, 21 and 22 for understanding of Static, Private ES6 Classes in theoretical.
Today we are focusing on few examples to understand the combination of both static + private.
That’s why the reason — you will notice the combination of static #fieldname or static #methodname
Note — Accessing directly static private field and method will return error.
Let’s Start with few examples to understand the concept of Static , Private and Static private
Enjoy Coding with Examples
private static field denoted by the
#prefix.
class MyCard {
// Private static field
static #address = 'test 123';
// Public static method to get the address
static getAddress() {
return MyCard.#address;
}
// Public static method to change the addres
static changeAddress(newAddress) {
MyCard.#address = newAddress;
}
}
// Accessing static private field and method
console.log(MyCard.getAddress()); // Output: test 123
MyCard.changeAddress('212 address');
console.log(MyCard.getAddress()); // Output: 212 address
MyCard.changeAddress('113 addres');
console.log(MyCard.getAddress()); // Output: 113 addres
Method 2 — How to Create Private Static Method
class MyCard {
// Private static field
static #address = 'test 123';
// Public static method to get the address
static getAddress() {
return MyCard.#address;
}
// Public static method to change the addres
static changeAddress(newAddress) {
MyCard.#address = newAddress;
}
}
// Accessing static private field and method
// Trying to access the private static field directly will result in an error
console.log(MyCard.#address);
// This would throw an error
// Trying to modify the private static field directly will also result in an error
MyCard.#address = 10; // This would throw an error
Error Use Cases
Accessing static private field and method result an error :)
class MyCard {
// Private static field
static #address = 'test 123';
static #lastName;
// Public static method to get the address
static getAddress() {
return MyCard.#address;
}
// Public static method to change the addres
static changeAddress(newAddress) {
MyCard.#address = newAddress;
}
static #changeLastName(lastName) {
MyCard.#lastName = lastName;
}
static updateLastName(lastName) {
MyCard.#changeLastName(lastName);
}
static getLastName() {
return MyCard.#lastName;
}
}
// Accessing static private field and method
// Trying to access the private static field directly will result in an error
//console.log(MyCard.#address);
console.log(MyCard.#lastName);
// This would throw an error
// Trying to modify the private static field directly will also result in an error
//MyCard.#address = 10; // This would throw an error
Accessing Method
class MyCard {
// Private static field
static #address = 'test 123';
static #lastName;
// Public static method to get the address
static getAddress() {
return MyCard.#address;
}
// Public static method to change the addres
static changeAddress(newAddress) {
MyCard.#address = newAddress;
}
static #changeLastName(lastName) {
MyCard.#lastName = lastName;
}
static updateLastName(lastName) {
MyCard.#changeLastName(lastName);
}
static getLastName() {
return MyCard.#lastName;
}
}
// Accessing static private field and method
// Trying to access the private static field directly will result in an error
//console.log(MyCard.#address);
//console.log(MyCard.#lastName);
// This would throw an error
// Attempting to access the private static method directly will result in an error
MyCard.#changeLastName('hgfhfghf new');
// This would throw an error
Go slow always with Classes Concept — :)
Happy Coding and Stay Tuned :)
Kind is cool !!!
Share knowledge to gain knowledge !!!!
Be kind and treat people with respect !!
If you find my blog helpful, then please subscribe, claps and follow to support :)







