"Cyber Space" consists of digital information, physical machines and programs ... and people, depending on how you want to look at it. 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.
Javascript is a programming language that's primarily used in web pages. This aspect of the language is something we'll look more closely at later. You'll hear Javascript referred to as a "scripting language" and its programs called "scripts". A Program (capital "P") is said to "run on the machine". Javascript programs are not meant to run on a physical machine, but instead to "run on the 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.
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.
|
|
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.
var s = "the"; s;can't is
easy with " "s, but not with ' 's. Try
entering '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? Inside a " "-delimited string, the " character has a special meaning, putting a \ character in front of it "escapes" that special meaning. Same thing works for a ' '-delimited 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.
"man" + "age" evaluates to "manage".
The + operator works this way regardless of whether we have
literal strings or variables that refer to strings. So, for
example, if var x = "man" and var y =
"age", then x + y evaluates to "manage".
Don't forget: with strings, + means concatenation, not addition.
[index].
So, if var x = "hard", the expression x[0]
evaluates to h, the expressions x[1]
evaluates to a, etc.
"" or ''.
.length at the end of a string to
create an expression that evaluates to a string's length.
So, for example, "hard".length evaluates to 4.
.charCodeAt(index)
Example: "hard".charCodeAt(0) yields 104,
because 104 is the ASCII value of the character h.
String.fromCharCode(number)
Example: String.fromCharCode(104); yields "h"
.substr(s,n). The s is
the start of the substring and n is the number of
characters to include. So
"Long string".substr(2,4);yields
"ng s"
typeof, and type conversionsNumber 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:
typeof(7+5);
typeof("7"+"5");
typeof(7*"5");
typeof(7+"5");
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:
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 Math functions we've already seen.