how to work with variables. In today's tutorial, we'll look onto basic data
types in more detail, specifically
Strings, numbers, and booleans.
avoid them and we should know how they work.
Basic data types are these:
(number with/without decimal),
We'll describe arrays and objects later and go through only the basic data types today.
String is a many characters saved in consecutive bytes in a
memory. Maximal length is not exactly specified, it depends on the interpreter.
In some variations it might be theoretically infinity and is limited only with
the RAM size.
As we learnt before, strings may be declared using double quotes or even single quotes:
let s1 = "some text"; let s2 = 'some text';
Quotes can be combined even inside of the
String. So if we e.g.
want to write a single quote we insert the
String into the double
let s1 = 'I like the " character, because I like how it looks.'; let s1 = "David, yes, that's me!";
If it's necessary to use the same quote in the text as is used to declare it,
the quote needs to be escaped, so the interpreter won't understand it as an end
of the string but just as an ordinary character. To do so we use the backslash
let x = "I like the \" character, because I like how it looks.";
Characters prefixed with the backslash are called escape sequence. Besides quotes we may write down a few more special characters, using the backslash, which would be otherwise hard to write. There's a list of them:
\f- Skip to another "page"
\n- New row
\r- Carriage return
\t- Horizontal tabulator
\v- Vertical tabulator
\\- Back slash
\'- Single quote
\"- Double quote
\0- Null character (also used to end
Functions and properties of strings
Using indexes we can access the individual characters in a
String the same way as in arrays. We'll speak about the array
later, so this is just an example ahead. The index is written between square
brackets right after the name of the variable. It's zero-based, so beware, if
you want the first character, it's
let x = "text"; document.write(x); // prints the first character of the string document.write(x); // prints the fourth character
The property (as the name suggests) carries the length of the string in characters.
let s = "text"; document.write(s.length); // Prints 4
If we read some text from the user, it may happen that they'll write a space
before or after it. And there're often some another whitespace characters around
the strings, e.g. end of line. The
trim() function trims those
whitespaces around the string so it doesn't complicate additional processing of
the string. If they're inside of the string, they'll be kept.
let s = " John Smith "; s = s.trim(); // Inside of s is now stored "John Smith"
This function replaces the searched value inside of the string with another one.
Changes all the letters in the string to the upper case or the lower case.
let s = "string"; s.toUpperCase(); // uppercase characters s.toLowerCase(); // lowercase characters
Joins two and more strings. This function is called automatically when we try
to add a
String using the
str1 = "Charles"; str2 = "John"; str3 = "Peter"; let joined = str1.concat(str2,str3); let joined2 = str1 + str2 + str3;
The last 2 lines do the same, they join the strings into a longer ones.
Functions return selected part of the
String, which is called
substring. Both do basically the same, they just differ in their parameters.
s = "Tutorials at ICT.social"; substring1 = s.substr(2, 6); // Returns 6 characters from the second position so "torial" substring2 = s.substring(2, 6); // Returns characters from the second to the 6th position, so "tori"
Splits the string into an array of strings using the specified character.
str.split("-"); // How-do-you-do ==  How  do  you  do
Returns the position of the specified substring.
str.indexOf("hello"); // returns the first index str.lastIndexOf("hello"); // returns the last index str.search("hello"); // returns the index of the searched string or the REGEX expression. We'll speak about regex later
Returns the match with the expression in the string. It's being used mostly for regular expressions (Regex), see further lessons.
Numbers can be written using two ways. First as usual:
let x = 10;
And the second using the scientific (exponential) notation:
let x = 10e5; // 10**5 = 100 000
We can do a lot of basic operations with numbers like summation,
multiplication, division... There's also a remainder after an integer division
(modulo) which we write using the
Functions and properties of numbers
Checks whether the object in the function parameter is a number or not.
"NaN" stands for a "Not a Number".
false according to if the parameter
is a number.
Truncates the number to the specified precision.
Converts the number to a string, so the number becomes a full-featured string.
Converts the number to the exponential (scientific) notation. E.g. for a
100 it'll be
contains very useful functions and properties, e.g. the number Pi,
max(), exponential functions... We'll show there a few of them.
We use the dot operator to access the functions and variables of libraries:
Math.PI // the number PI Math.E // the Euler's number Math.LN10 // Logarithm 10 Math.SQRT2 // contains the square root of 2 Math.max(1,10) // Returns the larger number. Opposite is the min() function Math.pow(4,3) // 4^3 == 64 Math.sqrt(9) // square root Math.random() // random number 0 - 1 Math.abs(x) // absolute value of the number Math.round(x) // rounds using the usual rules Math.ceil(x) // rounds always up Math.floor(x) // rounds always down Math.cos(x) // cosine Math.sin(x) // sine Math.log(x) // natural logarithm (the base is the Euler's number)
A logical data type, it can have only two values
false). Its value is either
true or false. It's being used to evaluate conditions and loops, we'll get to it
in further lessons.
Both data types stand for an empty variable. However, there's a difference.
Null is a non-created object, but
Undefined is not an
object at all.
No one has commented yet - be the first!