Main content
AP®︎/College Computer Science Principles
Storing strings in variables
When we're making a program, we often want to store sequences of letters, like storing “easy” as a game's difficulty level. In programming lingo, we call each letter a character and we call the sequence a string.
Here are examples of storing strings in JavaScript:
var player = "GrayFox";
var storeName = "Baskin Robbins 31 Flavors";
var grade = "A+";
A string can contain any number of characters, including none at all, and those characters can be letters, numbers, or symbols.
Notice how each string is surrounded by double quotes on either side. If you don’t put any quotes around a string, JavaScript will get confused.
🔍 What happens if you forget the quotes? See for yourself!
Uh oh, error!
Here's what happens on that first line: JavaScript looks for a variable named
GrayFox
. If it could actually find one, it would set the player
variable equal to the value of the GrayFox
variable. Since it doesn't find one, it errors instead. If you want it to store the literal string, you must surround it in quotes.✏️ See if you can fix the errors in the program above.
Pseudocode for strings
This pseudocode represents storing a string:
a ← "STRING"
Whenever you see that pseudocode, it means that the variable
a
(or whatever it's called) is storing the string inside the double quotes.For example, you might see pseudocode like this:
difficulty ← "medium"
That means the variable
difficulty
is storing the string "medium".Let's look at the equivalent code in a few textual languages:
language | code |
---|---|
JavaScript | var difficulty = "medium"; |
Python | difficulty = "medium" |
Java | String difficulty = "medium"; |
🙋🏽🙋🏻♀️🙋🏿♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!
Want to join the conversation?
- Is possible to get an overflow error in a string variable? I mean like overflow error in number variable.(1 vote)
- Not like numbers precisely. With numbers you have space and an encoding: For example, say you have 4 bits and you only encode positive numbers in binary:
Then you can encode all numbers between 0 and 15 (0000 and 1111), if you use that system for something like 8 + 9 you get overflow because 10001 cannot be represented with the for bits. So you might end up with 8 + 9 = 2 (0010).
Strings are stored as arrays, with each field in the array holding a character.
With higher-level languages that isn't an issue, with javascript, for example, you just tell your compiler you want a string and you don't have to do anything else on lower languages though you'll have to do that yourself.
So Khan would be stored in an array like so
K|a|h|n or rather the binary representation of those letters (K = 75 = 1001011 and so on)
Now depending on how much space you allocated for the array holding the String you might get overflow, that means you're telling the system to write characters to somewhere that wasn't originally assigned that function.
(e.g. you have an array with length 10 and you write something like array[20] = 'a').
That can cause unexpected behaviour and create security vulnerabilities in your code (vulnerability to buffer overflow attacks).
Unexpected behaviour means something happens, but you don't really know what.
Hope that helps and it isn't too long.(4 votes)
- How do you store quotation marks in strings if they're part of the syntax, and how are line breaks made?(2 votes)
- In JavaScript, the two main ways to store quote marks inside strings is to either "escape" the quotes with a backslash, or to use the opposite-style quote when storing the string.
This program shows both options: https://www.khanacademy.org/computer-programming/storing-strings-in-quotes/5145645492568064
You can make a newline in a string using \n. That's known as an "escape sequence". The backslash is used to start all escape sequences.
https://en.wikipedia.org/wiki/Escape_sequences_in_C(2 votes)
- If I wanted to create a variable in pseudocode,
would I do it like this:curly <- 10
or this:curly -> 10
(1 vote)- curly <- 10 would make more sense than curly -> 10(1 vote)
- How do you change the String to key.code and back again(1 vote)
- I'm curious why these examples are using
var
in JS as opposed tolet
.(1 vote)- The article might use "var" to be consistent with other articles/videos on Khan Academy that were created prior to the addition of "let" to the JS language in 2015. Of course, that is just a guess; I cannot say for certain.(1 vote)
- Hello, I have have just started backusing khan academy,such a great app to learn to code and all the information. My question is if I decide to write this code in pseudocode. Do I still have to use a semicolon after the quotation marks?
Example: var skittles <= "blue";(0 votes)- From the author:This pseudocode is the pseudocode style that is used by the AP CSP exam and it does not use semi-colons. If you aren't taking the AP CSP exam, I'd suggest learning JavaScript from our Intro to JS course instead- it's more interactive than these articles!(3 votes)
- bruh -alan romero(1 vote)
- Is there a way to execute code that is in a string? Like:
"function myFunction () {}" or "if(a){println(b);}"(0 votes)- Yes, you can use the eval function to run code stored as a string in JavaScript (e.g.
x = eval("3 + 5")
). However, YOU SHOULD NOT USE THIS FUNCTION! It is an enormous security risk and regarded as a terrible idea to use. Khan Academy does not even let you use this function in your projects.(1 vote)
- How do you remove a single character from a string variable?(0 votes)
- In JavaScript, you can use the
.slice
string modifier.println('Hello, world!'.slice(0, 1));
will return 'H'(0 votes)
- Is there anyway to store a string as the sum as two variables? For example, what if var firstName = "Spongebob" and var lastName = "Squarepants". Could I store var fullName (which would be Spongebob Squarepants) as the combination of two variables?(0 votes)
- Yes, you can. The concept that you are looking for is "string concatenation." You can concatenate (append one string to the end of another) two strings using the '+' operator.
Using the example you provided above, you would have:let fullName = firstName + ' ' + lastName;
When evaluated, fullName would be "Spongebob Squarepants".(0 votes)