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.
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.
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.
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,
var s = "the"; s;
'can't'in the interpreter and see what the problem is. Conversely, a string like the "it" is not important is easy to write with
' 's but not with
" "s. What about the string:
I didn't say "hello" to you!
How could I set a variable equal to such a string?
" "-delimited string, the
" character has a special
meaning, putting a
\ character in front of it "escapes" the
normal meaning of a character. Same thing works for a
string. Thus either of these two statements:
var s1 = "I didn't say \"hello\" to you!"; var s2 = 'I didn\'t say "hello" to you!';are OK. Of course this begs the question" "How do I write a
\character?" The answer is, escape it!
var s3 = "You can put \\ in a string!";I recommend putting all of those in the interpreter and seeing what values the variables get.
+operator is not addition, it is concatenation, i.e. it "glues" together the two strings, in order, to form a single new string. So, for example,
"man" + "age"evaluates to
+operator works this way regardless of whether we have string literals or variables that refer to strings. So, for example, if
var x = "man";and
var y = "age";, then
x + yevaluates to
"manage". Don't forget: with strings,
+means concatenation, not addition.
string[index]. So, if
var x = "hard";, the expression
h, the expression
''. The empty string is to strings as 0 is to numbers.
.lengthYou can put a
.lengthat the end of a string to create an expression that evaluates to a string's length. So, for example,
"hard".charCodeAt(0)yields 104, because 104 is the ASCII value of the character h.
String.fromCharCode(104);yields the string "h".
string.substr(s,n). The s is the start of the substring and n is the number of characters to include. So
"ng s"Again, the first character of a string starts at index 0.
typeof, and type conversions
String. There are other types (
typeof, that can be used to determine the type of a value. For example,
String. We can call
typeoffor any expression. You might ask what kind of value
typeofreturns. Here's how to find out: enter
typeof(typeof(4))into the interpreter. So now you know,
Stringthat gives the name of the type.
Here's some interesting ones to try:
+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
To say a word about the three other types:
Math.sqrt( )are examples of functions. Programmers can define their own functions as well as use the extensive library of built-in functions like the
Mathfunctions we've already seen.