C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
Photo
βΆοΈ What is circuit breaker and why to use it :
In our microservices application sometimes if one service stops working then it is waste of resources and time to call the entire services and get us back that a service is not working it is completely a waste of time and resources, so to avoid that we apply circuit breaker at the API gateway level so that we don't have to waste resources and time as api gateway is the entry point of any microservices application,so that at just entry level it shows that our service is not working,so to implement the functionality like that,to save resources and time,we need and use circuit breaker.
In our microservices application sometimes if one service stops working then it is waste of resources and time to call the entire services and get us back that a service is not working it is completely a waste of time and resources, so to avoid that we apply circuit breaker at the API gateway level so that we don't have to waste resources and time as api gateway is the entry point of any microservices application,so that at just entry level it shows that our service is not working,so to implement the functionality like that,to save resources and time,we need and use circuit breaker.
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
βΆοΈ What is circuit breaker and why to use it : In our microservices application sometimes if one service stops working then it is waste of resources and time to call the entire services and get us back that a service is not working it is completely a wasteβ¦
βΆοΈ There are 3 states of circuit breaker :
1. Open
2. Closed
3. Half Open
1. Open
2. Closed
3. Half Open
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
βΆοΈ There are 3 states of circuit breaker : 1. Open 2. Closed 3. Half Open
#How to add kafka in our project as a container through docker:
1.a) Add this property in docker-compose.yml file located in your root folder of your project or particular module :
zookeeper:
image: confluentinc/cp-zookeeper:7.5.0
hostname: zookeeper
container_name: zookeeper
ports:
- "2181:2181"
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
broker:
image: confluentinc/cp-kafka:7.5.0
container_name: broker
ports:
- "9092:9092"
- "29092:29092"
depends_on:
- zookeeper
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
schema-registry:
image: confluentinc/cp-schema-registry:7.5.0
hostname: schema-registry
container_name: schema-registry
depends_on:
- broker
ports:
- "8085:8081"
environment:
SCHEMA_REGISTRY_HOST_NAME: schema-registry
SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: 'broker:29092'
SCHEMA_REGISTRY_LISTENERS: http://schema-registry:8081
kafka-ui:
container_name: kafka-ui
image: provectuslabs/kafka-ui:latest
ports:
- "8086:8080"
depends_on:
- broker
environment:
KAFKA_CLUSTERS_NAME: local
KAFKA_CLUSTERS_BOOTSTRAPSERVERS: broker:29092
#KAFKA_CLUSTERS_SCHEMAREGISTRY: http://schema-registry:8081
DYNAMIC_CONFIG_ENABLED: 'true'
b) After that open terminal and go to where ur docker-compose.yml file is located and then run this command in terminal :
docker compose up -d
This will create a container of kafka in our project which we can use easily.
1.a) Add this property in docker-compose.yml file located in your root folder of your project or particular module :
zookeeper:
image: confluentinc/cp-zookeeper:7.5.0
hostname: zookeeper
container_name: zookeeper
ports:
- "2181:2181"
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
broker:
image: confluentinc/cp-kafka:7.5.0
container_name: broker
ports:
- "9092:9092"
- "29092:29092"
depends_on:
- zookeeper
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
schema-registry:
image: confluentinc/cp-schema-registry:7.5.0
hostname: schema-registry
container_name: schema-registry
depends_on:
- broker
ports:
- "8085:8081"
environment:
SCHEMA_REGISTRY_HOST_NAME: schema-registry
SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: 'broker:29092'
SCHEMA_REGISTRY_LISTENERS: http://schema-registry:8081
kafka-ui:
container_name: kafka-ui
image: provectuslabs/kafka-ui:latest
ports:
- "8086:8080"
depends_on:
- broker
environment:
KAFKA_CLUSTERS_NAME: local
KAFKA_CLUSTERS_BOOTSTRAPSERVERS: broker:29092
#KAFKA_CLUSTERS_SCHEMAREGISTRY: http://schema-registry:8081
DYNAMIC_CONFIG_ENABLED: 'true'
b) After that open terminal and go to where ur docker-compose.yml file is located and then run this command in terminal :
docker compose up -d
This will create a container of kafka in our project which we can use easily.
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
#How to add kafka in our project as a container through docker: 1.a) Add this property in docker-compose.yml file located in your root folder of your project or particular module : zookeeper: image: confluentinc/cp-zookeeper:7.5.0 hostname: zookeeperβ¦
2. Add kafka dependency as a dependency in your spring project :
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
2. Add kafka dependency as a dependency in your spring project : <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency>
3. Add these properties in application.properties file of your request producer service module :
#Kafka Properties.
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.template.default-topic=order-placed(topic_written_in_service_layer)
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
#Kafka Properties.
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.template.default-topic=order-placed(topic_written_in_service_layer)
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
3. Add these properties in application.properties file of your request producer service module : #Kafka Properties. spring.kafka.bootstrap-servers=localhost:9092 spring.kafka.template.default-topic=order-placed(topic_written_in_service_layer) spring.kafka.producer.keyβ¦
4. Dependencies to enable avro and schema-registry in our project(In Request Producer module/part of project):-
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-avro-serializer</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-schema-registry-client</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.11.3</version>
</dependency>
Also add plugin as follows :
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-avro-serializer</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-schema-registry-client</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.11.3</version>
</dependency>
Also add plugin as follows :
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
5. After that go to start.spring.io website download project with following dependencies and settings and plugins :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
5. After that go to start.spring.io website download project with following dependencies and settings and plugins : <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> β¦
<build>
<plugins>
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<plugins>
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
5. After that go to start.spring.io website download project with following dependencies and settings and plugins : <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> β¦
6. After downloading the Kafka consumer module(here we named it as notification-service),After that add these properties to your project's module's application.properties file(consumer's application.properties file) :
spring.application.name=notification-service
server.port=8084
#Mail Properties
spring.mail.host=sandbox.smtp.mailtrap.io
spring.mail.port=2525
spring.mail.username=a2adfdfdca22a5
spring.mail.password=f470820b1b0fad
#Kafka Consumer properties
spring.kafka.bootstrap-service=localhost:9092
spring.kafka.consumer.group-id=notificationService
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.consumer.properties.spring.json.trusted.packages=com.techie.microservices.order.event
spring.application.name=notification-service
server.port=8084
#Mail Properties
spring.mail.host=sandbox.smtp.mailtrap.io
spring.mail.port=2525
spring.mail.username=a2adfdfdca22a5
spring.mail.password=f470820b1b0fad
#Kafka Consumer properties
spring.kafka.bootstrap-service=localhost:9092
spring.kafka.consumer.group-id=notificationService
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.consumer.properties.spring.json.trusted.packages=com.techie.microservices.order.event
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
6. After downloading the Kafka consumer module(here we named it as notification-service),After that add these properties to your project's module's application.properties file(consumer's application.properties file) : spring.application.name=notificationβ¦
7. Go to project open in intellij/eclipse/SpringToolSuite and there make a package as order.event, there place this code with required variables as per your requirements and it should either match with schema defined in file defined as order-placed.avsc in avro folder in application.properties folder of module or with class defined with the same folder structure as in consumer's module as follows :
package com.techie.microservices.order.event;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderPlacedEvent {
private String orderNumber;
private String email;
}
or
Schema-registry file defined as order-placed.avsc in avro folder in application.properties folder of module:
{
"type": "record",
"name": "OrderPlacedEvent",
"namespace": "com.techie.microservices.order.event",
"fields": [
{ "name": "orderNumber", "type": "string" },
{ "name": "email", "type": "string" },
{ "name": "firstName", "type": "string" },
{ "name": "lastName", "type": "string" }
]
}
package com.techie.microservices.order.event;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderPlacedEvent {
private String orderNumber;
private String email;
}
or
Schema-registry file defined as order-placed.avsc in avro folder in application.properties folder of module:
{
"type": "record",
"name": "OrderPlacedEvent",
"namespace": "com.techie.microservices.order.event",
"fields": [
{ "name": "orderNumber", "type": "string" },
{ "name": "email", "type": "string" },
{ "name": "firstName", "type": "string" },
{ "name": "lastName", "type": "string" }
]
}
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
7. Go to project open in intellij/eclipse/SpringToolSuite and there make a package as order.event, there place this code with required variables as per your requirements and it should either match with schema defined in file defined as order-placed.avsc inβ¦
8. After that make a package named as service(you can keep it anything but most preferably you should write the name as service as it is providing service of sending mail) and then make a class in it(here it's NotificationService) and write the following code to enable consumer to consume the request and act accordingly here sending mail) like this :
package com.techie.microservices.notification.service;
import com.techie.microservices.order.event.OrderPlacedEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationService {
private final JavaMailSender javaMailSender;
@KafkaListener(topics = "order-placed")
public void listen(OrderPlacedEvent orderPlacedEvent){
log.info("Got Message from order-placed topic {}",orderPlacedEvent);
// Send email to the customer
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
messageHelper.setFrom("springshop@gmail.com");
messageHelper.setTo(orderPlacedEvent.getEmail());
messageHelper.setSubject(String.format("Your Order with OrderNumber %s is placed successfully",orderPlacedEvent.getOrderNumber()));
messageHelper.setText(String.format("""
Hi
Any msg in the body(here i'm providing message as Your order with order number %s is now placed successfully)
Best Regards
Spring Shop
""",
Any Dynamic msg you want to print in as %s variable Name(here it's orderPlacedEvent.getOrderNumber()));
};
try{
javaMailSender.send(messagePreparator);
log.info("Order Notification email sent!!");
}catch(MailException e){
log.error("Exception occurred when sending mailed");
throw new RuntimeException("Exception occured when sending mail to springshop@email.com",e);
}
}
}
package com.techie.microservices.notification.service;
import com.techie.microservices.order.event.OrderPlacedEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationService {
private final JavaMailSender javaMailSender;
@KafkaListener(topics = "order-placed")
public void listen(OrderPlacedEvent orderPlacedEvent){
log.info("Got Message from order-placed topic {}",orderPlacedEvent);
// Send email to the customer
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
messageHelper.setFrom("springshop@gmail.com");
messageHelper.setTo(orderPlacedEvent.getEmail());
messageHelper.setSubject(String.format("Your Order with OrderNumber %s is placed successfully",orderPlacedEvent.getOrderNumber()));
messageHelper.setText(String.format("""
Hi
Any msg in the body(here i'm providing message as Your order with order number %s is now placed successfully)
Best Regards
Spring Shop
""",
Any Dynamic msg you want to print in as %s variable Name(here it's orderPlacedEvent.getOrderNumber()));
};
try{
javaMailSender.send(messagePreparator);
log.info("Order Notification email sent!!");
}catch(MailException e){
log.error("Exception occurred when sending mailed");
throw new RuntimeException("Exception occured when sending mail to springshop@email.com",e);
}
}
}
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
8. After that make a package named as service(you can keep it anything but most preferably you should write the name as service as it is providing service of sending mail) and then make a class in it(here it's NotificationService) and write the following codeβ¦
9. After that Start all the services and it will work fine as required, sending emails whenever order event is triggered and request is send from the request producer to request consumer.
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
#How to add kafka in our project as a container through docker: 1.a) Add this property in docker-compose.yml file located in your root folder of your project or particular module : zookeeper: image: confluentinc/cp-zookeeper:7.5.0 hostname: zookeeperβ¦
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
8. After that make a package named as service(you can keep it anything but most preferably you should write the name as service as it is providing service of sending mail) and then make a class in it(here it's NotificationService) and write the following codeβ¦
GitHub
GitHub - ksubhamkrishna/Microservices_Project_With_Api_Gateway_RestTemplate_Swagger_Kafka_Prometheus_Loki_Grafana_kubernetes: β¦
Microservices_Project_With_Api_Gateway_Using Spring_Cloud_Gateway_MVC_RestTemplate_Swagger_Kafka_Prometheus_Loki_Grafana_kubernetes - ksubhamkrishna/Microservices_Project_With_Api_Gateway_RestTempl...
βΆοΈ Logical Operator Part-2: Truthy And Falsy Value in Javascript:-
/**
* Logical Operator with truthy and falsy values
* 1. OR
* 2. AND &&
*
* Truthy
* Falsy Values - "",0,null,undefined
*/
console.log(Boolean("Prakash"));
console.log(Boolean(""));
console.log(Boolean(null));
console.log(Boolean(undefined));
console.log(Boolean(0));
const firstName = "Prakash";
const nickName = "Anna";
console.log(firstName||nickName); // Output : Prakash
const emptyFirstName = "";
const FilledNickName = "Anna";
console.log(emptyFirstName|| FilledNickName); // Output : Anna
const FilledFirstName = "Prakash";
const EmptyNickName = "";
console.log(FilledFirstName|| EmptyNickName); //Output : Prakash
const emptyString = "";
const nullValue = null;
console.log(emptyString||nullValue);
const nullValue2 = null;
const emptyString2= "";
console.log( Name - ${nullValue2||emptyString2} );
console.log( Name - ${nullValue2emptyString2null} );
console.log( Name - ${nullValue2emptyString2null||"HiddenGeek"} ); //Short Circuiting
let a12 = 12;
let undefinedb;
console.log(a12+undefinedb);
let a = 12;
let b;
console.log(a+(b||0));
let a1 = 12;
let b1=3;
console.log(a1+(b1||0));
let a2 = 12;
let b2=null;
console.log(a2+(b2||0));
let a3 = 12;
let b3="";
console.log(a3+(b3||0));
const firstNameForAnd = "Prakash";
const firstNickNameForAnd = "Anna";
console.log(Name - ${firstNameForAnd && firstNickNameForAnd});
const firstNameForAnd1 = "Prakash";
const firstNickNameForAnd1 = null;
console.log(Name - ${firstNameForAnd1 && firstNickNameForAnd1});
const firstNameForAnd2 = "Prakash";
const firstNickNameForAnd2 = null;
console.log(Name - ${firstNameForAnd2 && firstNickNameForAnd2});
const firstNameForAnd3 = "Prakash";
const firstNickNameForAnd3 = "Anna";
console.log(Name - ${firstNameForAnd3 && firstNickNameForAnd3 && "HiddenGeek"});
Output : -
[Running] node "c:\Users\subham.krishna\Desktop\JavaScript\logical-operator-2.js"
true
false
false
false
false
Prakash
Anna
Prakash
null
Name -
Name - null
Name - HiddenGeek
NaN
12
15
12
12
Name - Anna
Name - null
Name - null
Name - HiddenGeek
/**
* Logical Operator with truthy and falsy values
* 1. OR
* 2. AND &&
*
* Truthy
* Falsy Values - "",0,null,undefined
*/
console.log(Boolean("Prakash"));
console.log(Boolean(""));
console.log(Boolean(null));
console.log(Boolean(undefined));
console.log(Boolean(0));
const firstName = "Prakash";
const nickName = "Anna";
console.log(firstName||nickName); // Output : Prakash
const emptyFirstName = "";
const FilledNickName = "Anna";
console.log(emptyFirstName|| FilledNickName); // Output : Anna
const FilledFirstName = "Prakash";
const EmptyNickName = "";
console.log(FilledFirstName|| EmptyNickName); //Output : Prakash
const emptyString = "";
const nullValue = null;
console.log(emptyString||nullValue);
const nullValue2 = null;
const emptyString2= "";
console.log( Name - ${nullValue2||emptyString2} );
console.log( Name - ${nullValue2emptyString2null} );
console.log( Name - ${nullValue2emptyString2null||"HiddenGeek"} ); //Short Circuiting
let a12 = 12;
let undefinedb;
console.log(a12+undefinedb);
let a = 12;
let b;
console.log(a+(b||0));
let a1 = 12;
let b1=3;
console.log(a1+(b1||0));
let a2 = 12;
let b2=null;
console.log(a2+(b2||0));
let a3 = 12;
let b3="";
console.log(a3+(b3||0));
const firstNameForAnd = "Prakash";
const firstNickNameForAnd = "Anna";
console.log(Name - ${firstNameForAnd && firstNickNameForAnd});
const firstNameForAnd1 = "Prakash";
const firstNickNameForAnd1 = null;
console.log(Name - ${firstNameForAnd1 && firstNickNameForAnd1});
const firstNameForAnd2 = "Prakash";
const firstNickNameForAnd2 = null;
console.log(Name - ${firstNameForAnd2 && firstNickNameForAnd2});
const firstNameForAnd3 = "Prakash";
const firstNickNameForAnd3 = "Anna";
console.log(Name - ${firstNameForAnd3 && firstNickNameForAnd3 && "HiddenGeek"});
Output : -
[Running] node "c:\Users\subham.krishna\Desktop\JavaScript\logical-operator-2.js"
true
false
false
false
false
Prakash
Anna
Prakash
null
Name -
Name - null
Name - HiddenGeek
NaN
12
15
12
12
Name - Anna
Name - null
Name - null
Name - HiddenGeek