When youcompare Java and JavaScript, you'll find many differences, but the semantics for performing conditional logic like if statements and switches is not one of them. Given that the syntax of JavaScript is based largely on that of its namesake, it shouldn't be surprising to discover that, like Java, JavaScript also employs the use of both the else and switch keywords for performing conditional logic.
There are many differences between Java and JavaScript, but the semantics of conditional logic is not one of them.
Using the else keyword, and else...if semantics, theroshambo (rock-paper-scissors) application would be coded like this:
<html>
<head>
<title>Roshambo!</title>
<script>
playRoshambo = function(clientGesture){
if (clientGesture == "rock") {
outcome = "tie";
} else if (clientGesture == "scissors") {
outcome = "loss";
} else if (clientGesture =="paper") {
outcome = "win";
}
document.getElementById('result')
.innerHTML = outcome;
}
</script>
</head>
<body>
Which one will it be?<br/>
<a href="#">rock</a>
<a href="#">paper</a>
<a href="#">scissors</a>
<div></div>
</body>
</html>
When we run the application, the rendering is the same as before. The logic may have changed, but the manner in which input is garnered and results are displayed stays the same (Figure 1).
Figure 1. The different possible outcomes of playing the roshambo game.
Similar to the Java implementation of roshambo,which uses the switch statement, along with corresponding case blocks and default positions, the JavaScript language provides the same facilities with the same semantics:
<html>
<head>
<title>Roshambo!</title>
<script>
playRoshambo = function(clientGesture){
switch (clientGesture) {
case "rock":
outcome = "tie";
break;
case "paper":
outcome= "win";
break;
case "scissors":
outcome = "loss";
break;
default:
outcome="error";
}
document.getElementById('result')
.innerHTML = outcome;
}
</script>
</head>
<body>
Which one will it be?<br/>
<a href="#">rock</a>
<a href="#">paper</a>
<a href="#">scissors</a>
<div></div>
</body>
</html>
Regardless of which implementation is chosen, gameplay remains the same (Figure 2). These changes only affect the logic, not the page rendering.
Figure 2. A successful, browser-based run of the roshambo game.
Java and JavaScript differ in a variety of ways, but for many of the core functions such as iterative looping and conditional processing, Java and JavaScript are very similar. There are always nuances between how business logic is performed in one or the other, but the similarities tend to outweigh the differences, making it relatively easy for Java developers to port their skills over to a browser, where JavaScript is king.
Many new frameworks are pushing for JavaScript on the server side. What has your experience been like moving from Java to JavaScript on the server?Let us know.
For those interested in learning Java, or for those who know Java and are interested in leveraging those skills in order to learn a complimentary language like JavaScript or Ruby, TheServerSide is providing a number of problem-driven tutorials that will help you master the fundamentals of these languages.
The catalog of tutorials currently includes the following:
An ADR is only as good as the record quality. Follow these best practices to establish a dependable ADR creation and maintenance ...
At some point, all developers must decide whether to refactor code or rewrite it. Base this choice on factors such as ...
API proxies and gateways help APIs talk to applications, but it can be tricky to understand vendor language around different ...
A principal engineer says Postman's Spec Hub will help the company shift to a spec-first API development process for its ...
AWS Kiro, a project developed by a 'small, opinionated team within AWS,' prioritizes spec-driven development in the workflows of ...
Docker is expanding the Docker Compose spec to accommodate AI agents in an effort to bring AI development closer to existing ...
AI is transforming PaaS with automation and cost-efficient features, but will it eventually replace cloud platforms? Industry ...
Even though Q-Day might be several years away, enterprises should develop a strategic plan to prepare for the future. Experts ...
Businesses can find security vulnerabilities when they push their workloads to the edge. Discover the pitfalls of cloud edge ...
Runtime security and tokenization stand to play a bigger role in attack surface management, a development that could influence ...
Check out the latest security news from the Informa TechTarget team.
How CISOs design and build their security teams is as important as the technology they select to safeguard their organizations' ...
Compare Datadog vs. New Relic capabilities including alerts, log management, incident management and more. Learn which tool is ...
Many organizations struggle to manage their vast collection of AWS accounts, but Control Tower can help. The service automates ...
There are several important variables within the Amazon EKS pricing model. Dig into the numbers to ensure you deploy the service ...