| Java | JavaScript | |
|---|---|---|
| Language Type | Object Oriented / Compiled | Object Oriented / Interpreted |
| Comments | // & /*...*/ | // & /*...*/ |
| Console Output | System.out.print() System.out.println() |
Console-> console.log(); HTML-> document.write(); |
| Dialog Output | JOptionPane.showMessageDialog(); | window.alert() |
| Concatenation Operator | + | + |
| Variables | int age = 43; double gpa = 3.5; String name = "Jim"; boolean isMale = true; char middleInit = 'G'; |
var age = 43; var gpa = 3.5; var name = "Jim"; var isMale = true; var middleInit = 'G'; |
| Console Input | name=reader.nextLine(); age=reader.nextInt(); gpa=reader.nextDouble(); |
Console const prompt=require('prompt-sync')(); name=prompt("Enter name:"); age=parseInt(prompt("Age:")); gpa=parseFloat(prompt("GPA:")); *may require console command: npm install prompt-sync HTML name=document.getElementById('name'); age=parseInt(document.getElementById('age')); gpa=parseFloat(document.getElementById('gpa')); |
| Dialog Input | name=JOptionPane.showInputDialog("Enter Name"); age=Integer.parseInt(JOptionPane.showInputDialog("Enter Age")); gpa=Double.parseDouble(JOptionPane.showInputDialog("Enter GPA")); |
name=window.prompt("Enter Name"); age=parseInt(window.prompt("Enter age")); gpa=parseFloat(window.prompt("Enter GPA")); |
| Math Methods | Math.sqrt(num); Math.pow(base,exp); Math.min(num1, num2); Math.max(num1, num2); Math.round(num); Math.ceil(num); Math.floor(num); Math.abs(num); Math.random(); Math.PI; |
Math.sqrt(num); Math.pow(base,exp); Math.min(num1, num2, ...); Math.max(num1, num2, ...); Math.round(num); Math.ceil(num); Math.floor(num); Math.abs(num); Math.random(); Math.PI; |
| String Methods | str.length; str.indexOf(string); str.lastIndexOf(string); str.charAt(num); str.toUpperCase(); str.toLowerCase(); str.substring(start, [end]); str.equals(string); str.equalsIgnoreCase(); str.compareTo(string); str.compareToIgnoreCase(string); |
str.length; str.indexOf(string); str.lastIndexOf(string); str.charAt(num); str.toUpperCase(); str.toLowerCase(); str.substring(start, [end]); str.substr(start, length); |
| Conditions | if(condition) code else code |
if(condition) code else code |
| For Loop | for(int x=0; x<5; x++) | for(var x=0; x<5; x++) |
| While Loop | while(condition) | while(condition) |
| do...while | do{...}while(condition); | do{...}while(condition); |
| Functions | public static int funcName(int age, double gpa){ code; return value;} |
function funcName (age, gpa){ code; return value;} |
| Arrays | int [] arr=new int[10]; arr[0]=53; |
var arr=[]; arr[0]=53; |
<script>...</script>
<script src='jscode.js'></script>
window.alert("output string here");document.write("output string here <br/>");
document.write("This will be on a new line.");var birthYear=parseInt(window.prompt("What year were you born?","Ex: 1999");
window.confirm('prompt');
if(condition){
...true code...
}
else{
...false code...
}
switch(variable){
case value: code; break;
case value: code; break;
default: code;
}
for(var x in arr){code}
function functionName(parameter list){
code
}
JavaScript functions can be called from other JavaScript code, from the href attribute of an a tag, or using events.
<script>
function over(){
document.getElementById('pic').src="picover.jpg";
}
function out(){
document.getElementById('pic').src="originalPic.jpg";
}
</script>
<img src='originalPic.jpg' id='pic' onmouseover='over()' onmouseout='out()' />
var arr=[];
arr.push(5); //arr={5}
arr.push(10); //arr={5,10}
arr.push(3); //arr={5,10,3}
arr.push(20); //arr={5,10,3,20}
document.write(arr[1]); //10
<div id='myDiv' onclick='change()'><div>
<script>
function change(){
var div=document.getElementById('myDiv');
div.setAttribute('width', '100px'); //sets the width of the div to 100px
div.style.backgroundColor='blue'; //changes the background color to blue
div.innerHTML('<em>Italic text</em>'); //add the words Italic text (in italics) to the inside of the div
}
</script>