//Cyber Battlefield/Programs – Statements & Variables

The Cyber Domain consists of digital information, physical machines, programs, and people. Of the non-human elements, programs are the most important to understand in order to get a handle on cyber security/warfare. This doesn't mean you need to be a programmer (though to be a true technical practitioner of cyber security you do), but you need some understanding of what programs are and the basics of how they work. In this lesson, that's what we'll start to do.

What are programs? What are programming languages? What is JavaScript?

The word program means lots of things in the context of computing. In previous classes, we described a program as a file that contains instructions for the computer in its native language of 0's and 1's. We might call that type of program an executable; i.e. data that is meant to be run on a CPU. Almost nobody writes this kind of program directly. Instead, we write programs in a programming language, which is a set of commands that can be automatically turned into an executable program; a file that contains code written in a programming language is also called source code. Programming languages should be easier for humans to use than the physical machine's native language of 0's and 1's.

JavaScript is a programming language that's primarily used in web pages. This aspect of the language is something we'll look at more closely later. You'll hear JavaScript referred to as a scripting language and its programs (source code) called scripts. A executable program is said to "run on the machine". JavaScript programs are not meant to run on a physical machine, but instead to "run in a browser", which itself is a program running on the physical machine. This kind of "program running within a program", which is actually extremely common, is often referred to as a script.

Expressions: Type, Value and Basic Operations

Browser Based JavaScript Interpreters
Many modern day browser have inline JavaScript interpreters. Follow the below steps to access your browsers JavaScript interpreter.
Google Chrome: Press Ctrl-Shift-J
Mozilla Firefox: Press Ctrl-Shift-K
Microsoft Internet Explorer: Press F12, Select Console icon

A calculator is interactive. You enter a mathematical expression, press =, and you get the value of the expression. Most programs don't work that way. The commands that make up the program are all written ahead of time in some file, and then the program executes all at once. Some kinds of programming languages must work that way. They are called compiled languages. Other languages are interpreted, which means that they can be evaluated interactively one command at a time, although that's still not the way it's usually done. JavaScript is interpreted, and we have an interpreter below that allows you to enter JavaScript a single expression or statement at a time, just like a calculator. Using the language interactively like this is a great way to learn.

Numbers, Expressions, Variables If you enter an expression in the input box to the right, the interpreter will evaluate the expression and write its value in the box below.
  • Numbers The simplest expression is a number. It evaluates to itself. Try this: input 58 and press enter. Not exciting, but it shows that 58 evaluates to 58. There are lots of arithmetic operations available, so you can enter complex mathematical expressions and the interpreter will evaluate them for you.
    basic arithmetic: +, -, *, / (the usual order of operations apply!) [note: % is the modulo (remainder) operator]
    math functions: Math.sin( ), Math.cos( ), Math.log( ), Math.exp( ), Math.sqrt( ), Math.floor( ), etc.
    math constants: Math.PI, Math.E, etc.
    Check out this complete reference of Math functions and constants.
    Question: What is the value of the expression:
    which, by the way, computes the side length for a square whose area is the same as a circle of radius 3.
  • Variables JavaScript lets you define variables, which are simply names that store values (recall, optionally store data). Once a name is declared as a variable, that name evaluates to the value it stores.
    declare variable named x as a variable: var x;
    assign variable named x a value: x = Math.sqrt(17);
    evaluate variable named x to get its value: x
    Usually, we declare and assign a variable simultaneously, e.g. var x = Math.sqrt(17);.
    Enter the following statements in turn.
    var r = 0.25;
    var cost = 57.99;
    cost = cost * (1 - r);
    cost = cost * (1 - r);
    You've just calculated how much a $57.99 item would cost with 25% off of a price already reduced by 25%.
    Note: = is assignment, not equality, which is why x = x + 1; makes sense.
  • What's with the ;? The ; is used to terminate statements. Statements are actions for the interpreter to perform. The action may be nothing more than evaluating an expression, but sometimes, like when we declare a variable with var, there's no expression — just an action to perform. JavaScript is pretty flexible about whether or not you use ;'s, but best practice is to just terminate every statement with one. BTW: you can put several statements on one line, e.g.
    var r = 0.25; var cost = 57.99; cost = cost * (1 - r);
  • Guess what — there are only seconds until 2019's graduation! Use the JavaScript interpreter to figure the time to graduation as the number of weeks, days, hours, minutes, seconds. Note: the % operator, which gives remainder, is really useful! For example, to start things off, if there are T seconds until graduation, then:
    s = T % 60; h = (T - s)/60;
    calculates that there are h hours and s seconds until graduation.
Input a JavaScript expression or statement and press Enter


JavaScript has more types of values than just Number. Another value type in JavaScript is String. Strings are used to represent sequences of characters. A string literal, analogous to a number literal like 76, is anything starting or ending with the " "s or ' 's. So, "hello world!" and 'hello world!' are both string literals. I cannot overestimate the importance in this course, in cyber security and in life of strings! Here are some things to know about strings in JavaScript.

The Best Jobs Need Programming
Five of the top 10 jobs in 2012 U.S. News & World Report's "25 Best Jobs" list require programming skills!

Types, typeof, and type conversions

We now know two types that a value can have, Number and String. There are other types (Boolean, Function and Object), though they'll be less relevant to us given the small amount we do with JavaScript. JavaScript has a special operator, typeof, that can be used to determine the type of a value. For example, typeof(7)Number, and typeof("foo")String. We can call typeof for any expression. You might ask what kind of value typeof returns. Here's how to find out: enter typeof(typeof(4)) into the interpreter. So now you know, typeof returns a String that gives the name of the type.

Here's some interesting ones to try:

What's going on here? JavaScript automatically converts numbers to strings and strings to numbers when it deems it to be necessary. If an arithmetic expression involves strings and any operation other than +, JavaScript will convert the string to a number. Because the + operation means something special to strings, JavaScript won't convert strings to numbers in that case. In fact, if you mix strings and numbers in a + expression, the numbers get converted to strings, which the last of the above examples demonstrates. This kind of type conversion that happens automatically behind the scenes is called an implicit conversion. There are explicit conversions too. If you wrap a string in Number( ) the interpreter attempts to convert the string to a number. For example, try 3 + Number("7") and compare what you get to 3 + "7". Similarly, if you wrap a number in String( ) the interpreter attempts to convert the number to string. For example, try typeof(String(365)).

To say a word about the three other types: