Amber Jain

Home | My GitHub | Resume/Vitae | Bio/Profile | Projects | Publications | Teaching | Contact
Collections | Book Recommendations | Weblogs | Notes | Miscellaneous | pub/


Javascript: Syntax

Notes:

Find the length of a string:

"Sample String".length

Comments in JS:

// This is a comment

Interactive JS:

confirm("Can you see this?");

Print out in a dialog:

alert("Alert!");

Input from user using dialog box:

prompt("What is your name?");

Datatypes in JS:

// numbers
2.123 + 2
// Strings
"Sample string"
// Booleans: true and false
10 < 3                   // evaluates to false
"Amber Jain".length > 5  // evaluates to true

Log to console:

console.log("Amber")

Comparison operators:

'if else' statement

if ( 100 >= 2 )
{
    console.log("You are good at math!");
}
else
{
    alert("Incorrect!");
}
console.log("Just letting you know: your program got past the if-else statement");

Math:

Substrings

"hello". substring(0, 2);  // outputs "he"

Variables

// Initialising variables
var myName = "Brendan";
var yearOfBirth = 1980;
var isOdd = false;
// Changing the values of variables
myName = "Brendan Eich";
yearOfBirth = 1961;
var isProgrammer = true;

Functions

var divideByThree = function (number) {
    var val = number / 3;
    console.log(val);
    return val
};
// Call the function by name
var result = divideByThree(6);
console.log(result);