Install this vscode extension for syntax highlight
- Variables
let name = "Roshan Acharya";
- Constants
const name = "Roshan Acharya";
-
Singleline
// This is a single line comment
-
Multiline
/* This is a multi line comment */
-
String
let name = "Roshan Acharya";
-
Number
let age = 100;
-
Boolean
let x = false; let y = true;
-
Null
let n = null;
-
Objects
let age = 100; let obj = { name: "Roshan Acharya", age }; print(obj.name); print(obj.age);
-
Arrays
let arr = ["Roshan", 100, true, false, null]; print(arr[0]); print(arr[1]);
let val;
if (x > 0) {
val = "positive";
else if (x < 0) {
val = "negative";
} else {
val = "equal";
}
print(val);
Curly braces are optional of body has single statement
let val;
if (x > 0) val = "positive";
else if (x < 0) val = "negative";
else val = "equal";
print(val);
Also parenthesis are optional in test condition.
let val;
if x > 0
val = "positive";
else if x < 0
val = "negative";
else
val = "equal";
print(val);
func log(val){
print(val);
}
Funcions return last expression by default
func add(x, y){
x + y;
}
Or you can use the return
keyword
func add(x, y){
return x + y;
}
You can define functions as a expressions too. Similar to function statements without function name.
const add = func (x, y) {
return x + y;
}
-
While
let x = 0; while (x < 20) { if (x == 10) continue; const y = calculateY(x); if (y == null) break; print("Value of x is: ", x); x++; }
-
For
for (let i = 0; i < 20; i++) {}
try {
const x = calculateValue();
if (x == null) {
throw "Something went wrong";
}
} catch (err) {
print(err);
}
-
export
func add(x, y) { x + y; } func sub(x, y) { x - y; } export { add, sub };
-
import
You can import both builtin module and file using import
import "vector" vec; // builtin module import "./ops.myriad" ops; // custom file const sum = ops.add(1, 2); const vec = vec.create(2, 4);
-
length
let name = "Roshan"; print(name.length()); // 6
-
split
let name = "Roshan Acharya"; print(name.split(" ")); // ["Roshan", "Acharya"]
-
replace
let name = "Roshan".replace("R", "r"); // roshan
-
uppercase
let name = "Roshan"; print(name.uppercase()); // ROSHAN
-
lowercase
let name = "Roshan"; print(name.lowercase()); // roshan
-
length
print([1, 2, 3].length()); // 3
-
join
print([1, 2, 3].join("-")); // 1-2-3
-
pop
["a", "b", "c"].pop(); // c
-
push
["a", "b", "c"].push("d"); // 4
-
includes
[1, 2, 3].includes(2); // true
-
foreach
[1, 2, 3].forEach(func (val, i) { print(val, i); })
-
format
format("My Name is {}", "Roshan");
-
print
print("Hello World");
-
input
input("> Enter your age ");
-
typeof
typeof(34);
- rand
- abs
- ceil
- floor
- round
- cos
- sin
- tan
-
stringify
json.stringify(obj);
-
parse
json.parse(str);
-
now
dt.now();
-
date
const date = dt.date("2000-01-01"); date.date(); date.year(); date.month();
-
server
const server = http.server(func (req, res){ if(req.url == "/json") { res.set_header("Content-Type", "application/json"); const msg = json.stringify({ message: "Hello World" }); res.send(msg); return; } if(req.method == "POST") { const body = json.parse(req.body); res.send("Your name is: " + body.name); return; } print(req.headers); res.send("Hello World"); }) server.listen(3000); print("Listening on port 3000");
-
write
fs.write("file.txt", "Hello");
-
read
fs.read("file.txt");
-
stat
fs.stat("file.txt");
-
mkdir
fs.mkdir("testdir");
-
readdir
fs.readdir("testdir");
-
rmrf
fs.rmrf("directory");
-
run
run_node.run(`console.log("Hello")`);