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?
- How do you store quotation marks in strings if they're part of the syntax, and how are line breaks made?(8 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(12 votes)
- Is possible to get an overflow error in a string variable? I mean like overflow error in number variable.(3 votes)
- 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.(5 votes)
- What is the difference between Javascript and Java?(2 votes)
- Java and Javascript are different programming languages; each one has its own purpose. Javascript is used heavily when creating websites while Java is used to create applications.(5 votes)
- Is there a reason you don't have to declare a variable in Python and you have to include the data type of a variable in Java?(1 vote)
- Yes, there is a fundamental difference in the way Python and Java handle variables and their data types. Python is a dynamically typed language, while Java is a statically typed language.
In Python, variables are created dynamically when you assign a value to them, and their type is inferred from the value that is assigned. This means that you don't need to declare the type of a variable explicitly, and you can change its type by assigning a value of a different type to it.
In Java, on the other hand, you need to declare the type of a variable explicitly when you create it, and that type cannot be changed later. This means that the compiler needs to know the type of every variable at compile time, which is why you need to declare the type of a variable in Java.(5 votes)
- 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!(5 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)
- the earth is the shape of the iphone 15 pro max(1 vote)
- 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.(2 votes)