Quick Index
Index
Comment Examples


We strive for simple code that describes itself (to another coder fluent in the given language).

However, concise comments added to clarify code when needed is helpful.

We'll demo the syntax for commenting js code.

We use "//" for line comments and "/*" and "*/" for the left and right bounds respectively for block comments

//This is a line comment
let a = 10;
prn("a: " + a);
/*this is a a block comment and can go
on multiple lines*/
a = 20;
prn("a: " + a);

Primitive Data


We'll play with some common primitive data.

We'll show how to initialize some primitive data here.

By primitive data, we mean non-object data (we'll learn about objects later).

let myNum1, myNum2, myString1, myString2, myString3,
		myBoolean1, myBoolean2;

//common number usage includes integers and floats
myNum1 = 5;
myNum2 = 10.99;

//strings
myString1 = 'we can use single quotes';
myString2 = "we can use double quotes";
myString3 = `or backticks (used for formatting)`;

//booleans (either true or false)
myBoolean1 = true;
myBoolean1 = false;

Operators


We'll play with some common operators.

String concatenation (joining) is done using the "+" operator.

let firstName, lastName, fullName;

firstName = 'Kofi';
lastName = 'Kingston';
//We add a space ' ' between the names
fullName = firstName + ' ' + lastName;
prn(fullName);
The common arithmetic operators are the familiar +, -, * and /.

let a, b, c, sum, diff, product, quotient;

a = 10;
b = 2;

sum = a + b;
diff = a - b;
product = a * b;
quotient = a / b;
The modulus (remainder) operator (%) often comes in handy.

let a, b, remainder;

a = 5;
b = 3;
remainder = a % b;
prn("5/3 remainder (expecting 2): " + remainder);
The increment operator (++) will be a close friend on our js journey.

It's a concise abbreviated form.
"count++" is the same as "count = count + 1"

"count--" is the same as "count = count - 1"

//This is a line comment
let count;

count = 10;
prn('count (before): ' + count);

count++;
prn('count (after "++"): ' + count);

count--;
prn('count (after "--"): ' + count);
Equality (equals) of prim values is done using the "===" operator and "not equals" is done using the "!==" operator.

There is also a "loose" version of each ("==" and "!=") but we generally prefer those above. We'll learn more details about the loose versions later.

//This is a line comment
let n1, n2, s1, s2, isEqual, isNotEqual;

n1 = 101;
n2 = 100 + 1;
//isEqual will be a boolean here (true/false)
isEqual = n1 === n2;
prn("isEqual (expecting true): " + isEqual);

s1 = 'foo';
s2 = 'oops';
//isEqual will be a boolean here (true or false)
isEqual = s1 === 22;
prn("isEqual (expecting false): " + isEqual);

s1 = 'foo';
s2 = 'oops';
isNotEqual = s1 !== 22;
prn("isNotEqual (expecting true): " + isNotEqual);

More Example Sources


Here are a couple (among many more) online related example sources: