Try It Now! and then
Run), you’ll see that it simply produces the output
When you say
In the above program,
'Hello, CommonLounge!' is a string. A string is a sequence of characters that can be processed by a computer. The string must always begin and end with the same character. This may be single (
') or double (
Always remember to put a semicolon
; at the end of every statement. It is not necessary but it is a good practice.
We’ll learn more about strings, numbers and operations in this tutorial.
What if we have some math inside
2 + 3? Hit the “Try it now” button to find out!
console.log(2 + 3);
3 + 4 - 5
5 * 4 / 2
console.log(3 + 4 - 5); console.log(5 * 4 / 2);
To perform exponential calculation, say 2 raised to the power 3, we type:
console.log(2 ** 3);
Have fun with this for a little while and then get back here :)
Each of the lines that you typed into the console so far is called an expression. Expressions can be much longer too. For example, try this:
console.log((5 + 2) * (7 - 1) / 3);
Expressions enclosed in parentheses get evaluated first. After all the parenthesized expressions are evaluated, the standard operator precedence is followed.
Here’s a step-by-step breakdown of how the above expression is evaluated.
(5 + 2) * (7 - 1) / 3 => (7) * (7 - 1) / 3 => 7 * (7 - 1) / 3 => 7 * (6) / 3 => 7 * 6 / 3 => 42 / 3 => 14
A sequence of characters is called a string. A string should also be enclosed within open and close inverted commas (
" ") or (
' '). Both opening and closing quotes should be the same for a string to be valid. For example,
'teststring"are not strings
Strings can be joined together. This is called concatenation of strings:
console.log('Hello ' + 'World!');
Notice how we had to end the first string (
'Hello ') with a space (
' '). If we did not, then our result would be
'HelloWorld!', which isn’t what we want.
If you need to put an apostrophe inside your string, you have two ways to do it.
Using double quotes:
console.log("Runnin' down the hill")
Runnin' down the hill
or escaping the apostrophe with a backslash (
console.log('Runnin\' down the hill')
Runnin' down the hill
Nice, huh? To see your name in uppercase letters, simply type:
You just used the
toUpperCase() method on your string! A method (like
"Commonlounge") once you call it.
console.log(). Here’s another one:
You can combine concatenation and repetition to form more complex strings
console.log('Hello' + '!'.repeat(3))
Wonder why sometimes you call functions with a
. at the end of a string (like
'!'.repeat(3)) and sometimes you first call a function and place the string in parentheses (like
console.log("Hello World!"))? Well, in some cases, functions belong to objects, like
toUpperCase(), which can only be performed on strings. In this case, we call the function a method.
If you want to know the number of characters in a string, you can use the
length attribute of a string.
Wonder why we don’t need parentheses -
() - for
Let’s try finding the number of digits in a number
Uncaught SyntaxError: Invalid or unexpected token
We got an error saying
Invalid or unexpected token. We got this error because numbers don’t have a
So what can we do? Since, we know that we can find length of a string, it makes sense to write our number as a string.
There’s another way to do this. We can use the
String() function to convert a number to a string.
Similarly, there’s also a
parseInt() function to convert from string to number (
Int stands for integer).
console.log(parseInt("1256") + 55)
What if we give it something that’s not a number? Let’s try:
NaN stands for Not a Number. It appears when we give a string to
parseInt that cannot be converted to a number.
Numbers with a decimal point are called floating point numbers or float for short. Similar to
parseFloat() converts the given string to a float.
What happens if you try adding a number to a string?
console.log(1 + " 2 3")
1 2 3
OK, enough of strings. So far you’ve learned about:
- operators – like
*, combine values to produce a new one
- functions and attributes – like
These are the basics of every programming language you learn. Ready for something harder? We bet you are!
Operator | Example | Result | Description ---------|---------|--------|------------------------------------------------ + | 4 + 5 | 9 | addition - | 4 - 5 | -1 | subtraction * | 4 * 5 | 20 | multiplication / | 8 / 3 | 2.666 | division % | 14 % 3 | 2 | modulo (remainder left after division) ** | 5 ** 2 | 25 | exponent
Play around with the functions that you’ve learned about so far. Try the following and try to explain the output.
You can try these out here: