STRINGS
In JavaScript, Strings are values made up of text and can contain letters,
numbers, symbols, punctuation, and even emojis!
// Learn How to Become a Web Developer with Pluralsight Skills
Single and Double Quotes in JavaScript Strings
Strings in JavaScript are contained within a pair of either single quotation
marks '' or double quotation marks "". Both quotes represent Strings but be
sure to choose one and STICK WITH IT. If you start with a single quote, you
need to end with a single quote. There are pros and cons to using both IE
single quotes tend to make it easier to write HTML within Javascript as you
don’t have to escape the line with a double quote.
EXAMPLE
'This is a string. 👏';
"This is the 2nd string. 💁";
Enclosing Quotation Marks
Let’s say you’re trying to use quotation marks inside a string. You’ll need to
use opposite quotation marks inside and outside of JavaScript single or
double quotes. That means strings containing single quotes need to use
double quotes and strings containing double quotes need to use single
quotes.
EXAMPLE
"It's six o'clock.";
'Remember to say "please" and "thank you."';
Alternatively, you can use a backslash \ to escape the quotation marks. This
lets JavaScript know in advance that you want to use a special character.
Here’s what that looks like reusing the examples above:
EXAMPLE
'It\'s six o\'clock.';
"Remember to say \"please\" and \"thank you.\"";
String Methods and Properties
Strings have their own built-in variables and functions, also known as
properties and methods. Here are some of the most common ones.
String Length
A string’s length property keeps track of how many characters it has.
EXAMPLE
"caterpillar".length;
OUTPUT
11
toLowerCase Method
A string’s toLowerCase method in JavaScript returns a copy of the string with
its letters converted to lowercase. Numbers, symbols, and other characters
are not affected.
EXAMPLE
"THE KIDS".toLowerCase();
OUTPUT
"the kids"
toUpperCase Method
A string’s toUpperCase method returns a copy of the string with its letters
converted to capitals. Numbers, symbols, and other characters are not
affected.
EXAMPLE
"I wish I were big.".toUpperCase();
OUTPUT
"I WISH I WERE BIG."
trim Method
A string’s trim method returns a copy of the string with beginning and ending
whitespace characters removed.
EXAMPLE
" but keep the middle spaces ".trim();
OUTPUT
"but keep the middle spaces"
LEARN
JAVASCRIPT
While HTML and CSS help create the design of a webpage, JavaScript helps
create functionality on a webpage. Other non-browser environments also use
JavaScript for more functionality.
Do not confuse Java and JavaScript. Though similar in name, the two are very
different programming languages.
Learn more about JavaScript below.
Strings
In JavaScript, Strings are values made up of text and can contain letters,
numbers, symbols, punctuation, and even emojis!
Learn more about strings
Numbers
Numbers are values that can be used in mathematical operations. You don’t
need any special syntax for numbers — just write them straight into
JavaScript.
Learn more about numbers
Booleans
In JavaScript, a boolean value is one that can either be TRUE or FALSE. If you
need to know “yes” or “no” about something, then you would want to use
the boolean function. It sounds extremely simple, but booleans are used all
the time in JavaScript programming, and they are extremely useful. Anything
that needs to be “on” or “off”, “yes” or “no”, “true” or “false”, or which just
has a temporary purpose, is usually a good fit for booleans.
Learn more about booleans
Operators
Operators are the symbols between values that allow different operations
like addition, subtraction, multiplication, and more.
Learn more about operators
Variables
Variables are named values and can store any type of JavaScript value.
Learn more about variables
Functions
JavaScript functions are reusable blocks of code that perform a specific task,
taking some form of input and returning an output.
Learn more about functions
Conditionals
Conditional statements control behavior in JavaScript and determine whether
or not pieces of code can run.
There are multiple different types of conditionals in JavaScript.
Learn more about conditionals
Arrays
Arrays are container-like values that can hold other values. The values inside
an array are called elements.
Array elements don’t all have to be the same type of value. Elements can be
any kind of JavaScript value — even other arrays.
Learn more about arrays
Objects
JavaScript objects are variables that contain multiple data values. The values
within a JS object are known as properties. Objects use keys to name values,
much like how is done with variables.
NUMBERS
Numbers are values that can be used in mathematical operations. You don’t
need any special syntax for numbers — just write them straight into
JavaScript.
EXAMPLE
12345;
Decimals and fractions
JavaScript doesn’t distinguish between whole numbers and decimals, so you
can use them together without having to convert from one to the other.
EXAMPLE
10 + 3.14159;
OUTPUT
13.14159
Fractions don’t exist in JavaScript, but you can rewrite them as division
problems using the division operator. Note that the resulting number is
always converted to decimals — just like with a calculator.
EXAMPLE
1 / 3;
OUTPUT
0.3333333333333333
Improper fractions use the division operator in the same way.
EXAMPLE
11 / 10;
OUTPUT
1.1
To use mixed numbers, you need to add the whole number and fraction.
EXAMPLE
1 + (4 / 3);
OUTPUT
2.333333333333333
Negative numbers
You can make a number negative by placing the - operator in front.
EXAMPLE
-3;
OUTPUT
-3;
You can also get a negative number by subtracting a number from a smaller
number.
EXAMPLE
5 - 7;
OUTPUT
-2